www.webdeveloper.com
Results 1 to 7 of 7

Thread: Don't know what is wrong with these php/html codes. they are meant to do the same

  1. #1
    Join Date
    Apr 2014
    Location
    Nigeria
    Posts
    5

    Don't know what is wrong with these php/html codes. they are meant to do the same

    I'm trying to build a site where students will be able to print their results online(a school project). After writing the html code and the php code, I connect it to the database(mysql) and its giving me this warning

    "mysql_connect()hp_network_getadresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\subject.php on line 51", and "Couldn't connect to server."
    And this is the php and html code(also javascript code).

    Code:
    "<?php // adduser.php
    
      // Start with the PHP code
      include_once 'dogs.php';
    
      $username = $names = $subject1 = $subject2 = $subject3 = $subject4 = $subject5 = $subject6 = $subject7 = $subject8 = $subject9 = "";
     if (isset($_POST['username']))
        $username = fix_string($_POST['username']);
      if (isset($_POST['names']))
        $names = fix_string($_POST['names']);
      if (isset($_POST['subject1']))
        $subject1  = fix_string($_POST['subject1']);
      if (isset($_POST['subject2']))
        $subject2 = fix_string($_POST['subject2']);
      if (isset($_POST['subject3']))
        $subject3 = fix_string($_POST['subject3']);
      if (isset($_POST['subject4']))
        $subject4= fix_string($_POST['subject4']);
    	if (isset($_POST['subject5']))
        $subject5= fix_string($_POST['subject5']);
    	if (isset($_POST['subject6']))
        $subject6= fix_string($_POST['subject6']);
    	if (isset($_POST['subject7']))
        $subject7= fix_string($_POST['subject7']);
    	if (isset($_POST['subject8']))
        $subject8= fix_string($_POST['subject8']);
    	if (isset($_POST['subject9']))
        $subject9= fix_string($_POST['subject9']);
    
      $fail  = validate_username($username);
      $fail .= validate_names($names);
      $fail .= validate_subject1($subject1);
      $fail .= validate_subject2($subject2);
      $fail .= validate_subject3($subject3);
      $fail .= validate_subject4($subject4);
      $fail .= validate_subject5($subject5);
      $fail .= validate_subject6($subject6);
      $fail .= validate_subject7($subject7);
      $fail .= validate_subject8($subject8);
      $fail .= validate_subject9($subject9);
    
      echo "<!DOCTYPE html>\n<html><head><title>Register</title>";
    
      if ($fail == "")
      {
        echo "</head><body>Form data successfully validated:
          $username, $names, $subject1, $subject2, $subject3, $subject4, $subject5, $subject6, $subject7, $subject8, $subject9.</body></html>";
    
        // This is where you would enter the posted fields into a database,
        // preferably using hash encryption for the password.
    	$connection = mysql_connect($user, $host, $password,$database)
    or die ("Couldn't connect to server.");
    $db = mysql_select_db($database, $connection)
    or die ("Couldn't select database.");
    $query = "INSERT INTO acct (username, names, subject1, subject2, subject3, subject4, subject5, subject6, subject7, subject8, subject9) VALUES 
    ( '$username', '$names', '$subject1', '$subject2', '$subject3', '$subject4', '$subject5', '$subject6', '$subject7', '$subject8', '$subject9')";
    mysql_query($sql);
    
    exit;
      }
    
      echo <<<_END
    
        <!-- The HTML/JavaScript section -->
    <html<head>
    
        <style>
          .signup {
            border: 1px solid #999999;
          font:   normal 14px helvetica; color:#444444;
          }
        </style>
    
        <script>
          function validate(form)
          {
    	  	 $fail  = validate_Username(form.username.value)
      		 $fail += validate_Names (form.names.value)
      		 $fail += validate_Subject1 (form.subject1.value)
      		 $fail += validate_Subject2 (form.subject2.value)
      		 $fail += validate_Subject3 (form.subject3.value)
      		 $fail += validate_Subject4 (form.subject4.value)
      		 $fail += validate_Subject5 (form.subject5.value)
      		 $fail += validate_Subject6 (form.subject6.value)
      		 $fail += validate_Subject7 (form.subject7.value)
      		 $fail += validate_Subject8 (form.subject8.value)
      	
    	 $fail += validate_Subject9 (form.subject9.value)
                  
            if (fail == "")     return true
            else { alert(fail); return false }
          }
    
          function validateUsername(field)
          {
            return (field == "") ? "No Username was entered.\n" : ""
          }
    
          function validateNames(field)
          {
            return (field == "") ? "No Names was entered.\n" : ""
          }
    
          function validateSubject1(field)
          {
            return (field == "") return "No Subject was entered.\n"
          }
    	  function validateSubject2(field)
          {
            return (field == "") return "No Subject was entered.\n"
          }
    	  function validateSubject3(field)
          {
            return (field == "") return "No Subject was entered.\n"
          }
    	  function validateSubject4(field)
          {
            return (field == "") return "No Subject was entered.\n"
          }
    	  function validateSubject5(field)
          {
            return (field == "") return "No Subject was entered.\n"
          }
    	  function validateSubject6(field)
          {
            return (field == "") return "No Subject was entered.\n"
          }
    	  function validateSubject7(field)
          {
            return (field == "") return "No Subject was entered.\n"
          }
    	  function validateSubject8(field)
          {
            return (field == "") return "No Subject was entered.\n"
          }
    	  function validateSubject9(field)
          {
            if (field == "") return "No Subject was entered.\n"
          }
          
        </script>
      </head>
      <body>
    
        <table border="0" cellpadding="2" cellspacing="5" bgcolor="#eeeeee">
          <th colspan="2" align="center">Signup Form</th>
    
            <tr><td colspan="2">Fill The Form<br>
              enter the subjects you offered: <p><font color=red size=1><i>$fail</i></font></p>
            </td></tr>
    
          <form method="post" action="subject.php" onSubmit="return validate(this)">
            <tr><td>Username</td>
              <td><input type="text" maxlength="32" name="username" value="username">
            </td></tr><tr><td>names</td>
              <td><input type="text" maxlength="32" name="names"  value="names">
            </td></tr>
            <tr><td>Subject1</td>
              <td><input type="text" maxlength="30" name="subject1" value="subject1">
            </td></tr>
    		<tr><td>Subject2</td>
              <td><input type="text" maxlength="30" name="subject2" value="subject2">
            </td></tr>
    		<tr><td>Subject3</td>
              <td><input type="text" maxlength="30" name="subject3" value="subject3">
            </td></tr>
    		<tr><td>Subject4</td>
              <td><input type="text" maxlength="30" name="subject4" value="subject4">
            </td></tr>
    		<tr><td>Subject5</td>
              <td><input type="text" maxlength="30" name="subject5" value="subject5">
            </td></tr>
    		<tr><td>Subject6</td>
              <td><input type="text" maxlength="30" name="subject6" value="subject6">
            </td></tr>
    		<tr><td>Subject7</td>
              <td><input type="text" maxlength="30" name="subject7" value="subject7">
            </td></tr>
    		<tr><td>Subject8</td>
              <td><input type="text" maxlength="30" name="subject8" value="subject8">
            </td></tr>
    		<tr><td>Subject9</td>
              <td><input type="text" maxlength="30" name="subject9" value="subject9">
            </td></tr>
    		<tr><td colspan="2" align="center"><input type="submit"
              value="Register"></td></tr>
          </form>
        </table>
      </body>
    </html>
    
    _END;
    
      // The PHP functions
      function validate_username($field)
          {
            return ($field == "") ? "No Username was entered<br>" : "";
          }
    
          function validate_names($field)
          {
            return ($field == "") ? "No Names was entered<br>" : "";
          }
    
          function validate_subject1($field)
          {
            return ($field == "") ? "No Subject was entered<br>" :"";
          }
    	  function validate_subject2($field)
          {
            return ($field == "") ? "No Subject was entered<br>" :"";
          }
    	  function validate_subject3($field)
          {
            return ($field == "") ? "No Subject was entered<br>" :"";
          }
    	  function validate_subject4($field)
          {
            return ($field == "") ? "No Subject was entered<br>" :"";
          }
    	  function validate_subject5($field)
          {
            return ($field == "") ? "No Subject was entered<br>" :"";
          }
    	  function validate_subject6($field)
          {
            return ($field == "") ? "No Subject was entered<br>" :"";
          }
    	  function validate_subject7($field)
          {
            return ($field == "") ? "No Subject was entered<br>" :"";
          }
    	  function validate_subject8($field)
          {
            return ($field == "") ? "No Subject was entered<br>" :"";
          }
    	  function validate_subject9($field)
          {
    	  
            return ($field == "") ? "No Subject was entered<br>" :"";
          }
      
      function fix_string($string)
      {
        if (get_magic_quotes_gpc()) $string = stripslashes($string);
        return htmlentities ($string);
      }
    ?>"
    Last edited by jedaisoul; 07-04-2014 at 04:13 PM. Reason: code tags added

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    Look at the arguments you provide here:
    PHP Code:
    $connection mysql_connect($user$host$password$database
    Now look at the function prototype from the manual page:
    resource mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false [, int $client_flags = 0 ]]]]] )
    Your first two arguments are transposed, and the $database argument does not belong there at all -- unless you meant to be using mysqli_connect() (in which case you would at least not be using a deprecated PHP extension)?
    Last edited by NogDog; 07-03-2014 at 12:43 PM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    724
    Where did you learn to program - or I should say - attempt to learn to program? This is the silliest code I've ever seen. You are creating the same function over and over and giving them all unique names. WHY? They all do the same thing!

    Your html inputs are assigning values of the name of the input that are meaningless and that will show up in your php as variable values which you will then accept since they are not null.

    You have php vars in your js code. No Can Do.

    This code is a mess - cease and desist until you better understand how programming works.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  4. #4
    Join Date
    Apr 2014
    Location
    Nigeria
    Posts
    5
    But that is all what I read in the ebooks I bought. Who has a clue of how I should go about it?

  5. #5
    Join Date
    Apr 2014
    Location
    Nigeria
    Posts
    5
    And this is the second method I used.
    subject.php and the second will do code will be in an include file.

    "<?php
    include_once'dogs.php';
    foreach($_POST as $field => $value) #62
    {
    if ($value == "") #66
    {
    unset($_GET['subject2']);
    $message_new = "Required information is missing.
    Please try again.";
    include("subject2_form.php");
    exit();
    }
    if (ereg("(Name)",$field))
    {
    if (!ereg("^[A-Za-z' -]$",$value))
    {
    unset($_GET['subject2']);
    $message_new = "$field is not a valid name.
    Please try again.";
    include("subject2_form.php");
    exit();
    }
    }
    $$field = strip_tags(trim($value));
    }//end foreach
    $connection = mysql_connect($user, $host, $password,$database)
    or die ("Couldn't connect to server.");
    $db = mysql_select_db($database, $connection)
    or die ("Couldn't select database.");
    if ($value == "") #66
    {
    unset($_GET['subject2']);
    $message_new = "Required information is missing.
    Please try again.";
    include("subject2_form.php");
    exit();
    }
    else{
    $query = "INSERT INTO acct (username, names, subject1, subject2, subject3, subject4, subject5, subject6, subject7, subject8, subject9) VALUES
    ( '$username', '$names', '$subject1', '$subject2', '$subject3', '$subject4', '$subject5', '$subject6', '$subject7', '$subject8', '$subject9')";
    mysql_query($sql);
    }
    ?>"

    This is the second part.

    "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    <FORM METHOD="POST" ACTION="subject2.php">
    <?php #25
    if (isset($subject2))
    echo "$subject2";
    ?>
    <p>Username: <INPUT TYPE="text" NAME="username" SIZE="35"value="<?php echo @$username ?>"maxlength="20">
    <p>names: <INPUT TYPE="text" NAME="names" SIZE="50"value="<?php echo @$names ?>"maxlength="80">
    <p>Password: <INPUT TYPE="password" NAME="password" SIZE="35"value="<?php echo @password ?>"maxlength="20">
    <p>Subject1: <INPUT TYPE="text" NAME="subject1" SIZE="35"value="<?php echo @$subject1; ?>"maxlength="20">
    <p>Subject2: <INPUT TYPE="text" NAME="subject2" SIZE="35"value="<?php echo @$subject2; ?>"maxlength="20">
    <p>Subject3: <INPUT TYPE="text" NAME="subject3" SIZE="35"value="<?php echo @$subject3; ?>"maxlength="20">
    <p>Subject4: <INPUT TYPE="text" NAME="subject4" SIZE="35"value="<?php echo @$subject4; ?>"maxlength="20">
    <p>Subject5: <INPUT TYPE="text" NAME="subject5" SIZE="35"value="<?php echo @$subject5; ?>"maxlength="20">
    <p>Subject6: <INPUT TYPE="text" NAME="subject6" SIZE="35"value="<?php echo @$subject6; ?>"maxlength="20">
    <p>Subject7: <INPUT TYPE="text" NAME="subject7" SIZE="35"value="<?php echo @$subject7; ?>"maxlength="20">
    <p>Subject8: <INPUT TYPE="text" NAME="subject8" SIZE="35"value="<?php echo @$subject8; ?>"maxlength="20">
    <p>Subject9: <INPUT TYPE="text" NAME="subjec92" SIZE="35"value="<?php echo @$subject9; ?>"maxlength="20">
    <p><input type="submit"value="Enter Subjects">

    </body>
    </html>"

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    Query about your code here...
    PHP Code:
    connection mysql_connect($user$host$password,$database) or die ("Couldn't connect to server."); 
    should it not be
    PHP Code:
    connection mysql_connect($host$user$password) or die ("Couldn't connect to server."); 
    Another observation is that you are using the older mysql_ and not the newer mysqli_ methods.

    Finally, when posting please use forum tags to wrap around your code, it makes it easier to read the code.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  7. #7
    Join Date
    Apr 2014
    Location
    Nigeria
    Posts
    5
    Thanks, You know am a newbie here

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles