/    Sign up×
Community /Pin to ProfileBookmark

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():php_network_getadresses: getaddrinfo failed: No such host is known. in C:xampphtdocssubject.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);
}
?>”
[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 03.2014 — Look at the arguments you provide here:
[code=php]
$connection = mysql_connect($user, $host, $password, $database)
[/code]

Now look at the function prototype from the manual page:
[FONT=Courier New]resource mysql_connect ([ string [B]$server[/B] = ini_get("mysql.default_host") [, string [B]$username[/B] = ini_get("mysql.default_user") [, string [B]$password[/B] = ini_get("mysql.default_password") [, bool [B]$new_link[/B] = false [, int [B]$client_flags[/B] = 0 ]]]]] )[/FONT]
[/quote]

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)?
Copy linkTweet thisAlerts:
@ginerjmJul 03.2014 — 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.
Copy linkTweet thisAlerts:
@oluwaauthorJul 04.2014 — But that is all what I read in the ebooks I bought. Who has a clue of how I should go about it?
Copy linkTweet thisAlerts:
@oluwaauthorJul 04.2014 — 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>"
Copy linkTweet thisAlerts:
@rootJul 04.2014 — Query about your code here... [code=php]connection = mysql_connect($user, $host, $password,$database) or die ("Couldn't connect to server.");[/code]

should it not be [code=php]connection = mysql_connect($host, $user, $password) or die ("Couldn't connect to server.");[/code]

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.
Copy linkTweet thisAlerts:
@oluwaauthorJul 04.2014 — Thanks, You know am a newbie here
×

Success!

Help @oluwa spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...