Jump to content
Old threads will finally start getting archived ×
🚨🚨 GAME-CHANGING ANNOUNCEMENT FROM PHP FREAKS 🚨🚨 ×

Newbie question/help


Gemini 🤖

Recommended Posts

I have just set up serve on my pc and got php and mysql and stuff

 

I am trying to get started by using a very simple script to gather data from a form and add it to the database and then read all teh data into a table. I am using a book and i have all the code right i am sure but it is not working. What i do not understand is that it worked the first tiem i did it but not anymore :S

 

Can anyone maybe help me by testign the files on there server or somthing? or shall i post code on this site?

 

OR does anyone know of anything (any settings) that i might have to change? Thanks veyr much James

 

I know i have the db and table set up ok!

 

THE FORM

<form name="form1" method="post" action="handle_form.php">

 <p>First Name 

   <input name="Array[firstName]" type="text" id="sadfsdf">

 </p>

 <p>Last Name 

   <input name="Array[lastName]" type="text" id="Array[firstName]">

 </p>

 <p>EMail Address 

   <input type="text" name="Array[email]">

 </p>

 <p>DOB 

   <input type="text" name="Array[DOB]">

 </p>

 <p>Info 

   <textarea name="Array[info]"></textarea>

 </p>

 <p>

   <input type="submit" name="Submit" value="Submit">

 </p>

</form>

 

HANDLE PAGE



<?php



//Database Info

$host = "localhost";

$user = "";

$password = "";

$dbName = "jmusers";

$tableName = "userInfo";



//Connect to the database

$link = mysql_connect($host, $user, $password);



$query = "INSERT into $tableName values 

(\'0\', \'$Array[firstName]\',\'$Array[lastName]\',\'$Array[email]\',\'$Array[DOB]\',\'$Array[info]\')";



print "The query is:<BR>$query<P>n";



if (mysql_db_query($dbName, $query, $link)) {

print "The query was successfully executed<br><br>";

}

else {

print "The query could not be executed";

}



//close the link to the database

mysql_close($link);

?>

 

GET DB DATA PAGE



<?php

//Database Info

$host = "localhost";

$user = "";

$password = "";

$dbName = "jmusers";

$tableName = "userInfo";

//Connect to the database

$link = mysql_connect($host, $user, $password);



$query = "SELECT * from $tableName";

$result = mysql_db_query ($dbName, $query, $link);



//creat a table to show the table results in

print "<table border=1 width ="100%" cellspacing=2 cellpadding=2 align=center>n";

print "<tr align=center valign=top>n";

print "<td align=center valigntop>Name</td>n";

print "<td align=center valigntop>EMail</td>n";

print "<td align=center valigntop>DOB</td>n";

print "<td align=center valigntop>Comments/Info</td>n";

print "</tr>n";



//fetch the results from the database

while ($row = mysql_fetch_array($result)) {

print "<tr align=center valign=top>n";

print "<td align=center valign=top>$row[firstName] $row[firstName]</td>n";

print "<td align=center valign=top>$row[email]</td>n";

print "<td align=center valign=top>$row[DOB]</td>n";

print "<td align=center valign=top>$row[info]</td>n";

print "</tr>n";

}

//close the link to the database

mysql_close($link);



print "</table>n";



?>

Link to comment
https://forums.phpfreaks.com/topic/187-newbie-questionhelp/
Share on other sites

If you\'ve installed the latest version of PHP (4.3), you\'re working with register_globals off.

 

On the handle page, add this to the beginning of the code:[php:1:8db01511b1]$Array = $HTTP_POST_VARS[\'Array\'];[/php:1:8db01511b1]

 

You\'ll want to read up on working with register_globals off: http://www.php.net/manual/en/security.regi...sterglobals.php

 

Hope that helps.

Link to comment
https://forums.phpfreaks.com/topic/187-newbie-questionhelp/#findComment-539
Share on other sites

http://code.phirebrush.com/ref/form01

 

index.html:

<form method="post" action="handle_form.php">

 <p>First Name

   <input type="text" name="firstname">

 </p>

 <p>Last Name

   <input type="text" name="lastname">

 </p>

 <p>EMail Address

   <input type="text" name="email">

 </p>

 <p>DOB

   <input type="text" name="dob">

 </p>

 <p>Info

   <textarea name="info"></textarea>

 </p>

 <p>

   <input type="submit" value="Submit">

 </p>

</form>

 

handle_form.php:[php:1:9b6ade3a63]<?php

 

//Get POST Variables

$firstname = $HTTP_POST_VARS[\'firstname\'];

$lastname = $HTTP_POST_VARS[\'lastname\'];

$email = $HTTP_POST_VARS[\'email\'];

$dob = $HTTP_POST_VARS[\'dob\'];

$info = $HTTP_POST_VARS[\'info\'];

 

//Connect to the database

$link = mysql_connect($host, $user, $password) or die(\"<code>Error: <i>\".mysql_error().\"</i></code>\");

mysql_select_db($dbName,$link) or die(\"<code>Error: <i>\".mysql_error().\"</i></code>\");

$q = mysql_query(\"INSERT INTO `\".$tableName.\"` (`firstname`,`lastname`,`email`,`dob`,`info`) VALUES (\'\".$firstname.\"\',\'\".$lastname.\"\',\'\".$email.\"\',\'\".$dob.\"\',\'\".$info.\"\')\",$link) or die(\"<code>Error: <i>\".mysql_error().\"</i></code>\");

 

mysql_close($link);

header(\"Location: results.php\");

 

?>[/php:1:9b6ade3a63]

 

results.php:[php:1:9b6ade3a63]<?php

 

$link = mysql_connect($host, $user, $password) or die(\"<code>Error: <i>\".mysql_error().\"</i></code>\");

mysql_select_db($dbName,$link) or die(\"<code>Error: <i>\".mysql_error().\"</i></code>\");

$q = mysql_query(\"SELECT * FROM `\".$tableName.\"` ORDER BY `id` DESC\",$link) or die(\"<code>Error: <i>\".mysql_error().\"</i></code>\");

 

//creat a table to show the table results in

print \"<table border=1 width =\"100%\" cellspacing=2 cellpadding=2 align=center>n\";

print \"<tr align=center valign=top>n\";

print \"<td align=center valigntop>Name</td>n\";

print \"<td align=center valigntop>EMail</td>n\";

print \"<td align=center valigntop>DOB</td>n\";

print \"<td align=center valigntop>Comments/Info</td>n\";

print \"</tr>n\";

 

//fetch the results from the database

while ($row = mysql_fetch_array($q)) {

print \"<tr align=center valign=top>n\";

print \"<td align=center valign=top>\".$row[\'firstname\'].\" \".$row[\'lastname\'].\"</td>n\";

print \"<td align=center valign=top>\".$row[\'email\'].\"</td>n\";

print \"<td align=center valign=top>\".$row[\'dob\'].\"</td>n\";

print \"<td align=center valign=top>\".$row[\'info\'].\"</td>n\";

print \"</tr>n\";

}

//close the link to the database

mysql_close($link);

 

print \"</table>n\";

 

?>[/php:1:9b6ade3a63]

 

Hope that helps.

Link to comment
https://forums.phpfreaks.com/topic/187-newbie-questionhelp/#findComment-541
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.