Jump to content

INSERT INTO DATABASE THROUGH A FORM


chaddsuk

Recommended Posts

Hi

 

Im having trouble inserting data into my database using a html form, can someone take a look at where im am going wrong, i think i have my syntax wrong within my table.

 

copied and pasted below

 

cheers

 

chris

 

<?php

//Assign contents of form to variables

$name = $_POST['name'];

$age = $_POST['age'];

 

?>

 

 

 

<center><form action="<?PHP ($_SERVER[DOCUMENT_SELF]) mysql_query ( "INSERT INTO users (name, age) VALUES ('$name','$age')"; ?>" method="post">

<input type="text" name="name" size="20" /> Name<br />

<input type="text" name="age" size="20" /> Age<br />

<input type="submit" value="Store in database" /><input type="reset" value="Reset fields" />

</form></center>[/color]

Link to comment
Share on other sites

Ok realised i completely messed up the form, its still wrong but am i getting closer?

 

 

<center><form action="<?PHP (echo $_SERVER[php_SELF]) mysql_query ("INSERT INTO users (name, age) VALUES ('$name','$age')}"?> method="post">

<input type="text" name="name" size="20" /> Name<br />

<input type="text" name="age" size="20" /> Age<br />

<input type="submit" value="Store in database" /><input type="reset" value="Reset fields" />

</form></center>

Link to comment
Share on other sites

<?php

//Assign contents of form to variables

$name = $_POST['name'];

$age = $_POST['age'];

?>

 

Replace the code above with this

 

<?php

 

if(isset($_POST['submit'])){

 

$name = $_POST['name'];

$age = $_POST['age'];

 

 

 

include ('connect.php'); // this is where you connect to the database the connect.php

    contents the script to connect to the database.

 

$myQuery = "INSERT INTO users VALUES (null, '$name', $age)";

 

$result = mysql_query($myQuery);

 

 

if(mysql_affected_rows($dbc)>0){

 

echo "Successfully Added";

}

else{

echo "Process failed. Error ecounter";

 

}

 

  }

?>

 

 

<center><form action="<?PHP ($_SERVER[DOCUMENT_SELF]) mysql_query ( "INSERT INTO users (name, age) VALUES ('$name','$age')"; ?>" method="post">

<input type="text" name="name" size="20" /> Name

 

<input type="text" name="age" size="20" /> Age

 

<input type="submit" value="Store in database" /><input type="reset" value="Reset fields" />

</form></center>[/color]

 

and alse this one, you don't have to put query into a form it will drive you crazy. replace it with this one

<?php echo $_SERVER['PHP_SELF']; ?> -- this will tell the browser to add to this page.. i hope it will help you.. please read books about MySQL and PHP and also read some online toturials.

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

<table>

<tr>

<td>NAme:</td>

<td><input type="text" name="name"></td>

</tr>

<tr>

<td>Age:</td>

<td><input type="text" name="age"></td>

</tr>

<tr>

<td><input type="submit name="submit" value="Add"></td>

<td><input type="reset" name="clear" value="Reset"></td>

</tr>

</table></form>

Link to comment
Share on other sites

here it goes the whole code. I hope it will help you. ;D

 

<?php

 

 

if(isset($_POST['submit'])){

 

$name = $_POST['name'];

$age = $_POST['age'];

 

 

 

include ('connect.php'); // this is where you connect to the database the connect.php

    contents the script to connect to the database.

 

$myQuery = "INSERT INTO users VALUES (null, '$name', $age)";

 

$result = mysql_query($myQuery);

 

 

if(mysql_affected_rows($dbc)>0){

 

echo "Successfully Added";

}

else{

echo "Process failed. Error ecounter";

 

}

 

  }

?>

 

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

<table>

<tr>

<td>Name:</td>

<td><input type="text" name="name"></td>

</tr>

<tr>

<td>Age:</td>

<td><input type="text" name="age"></td>

</tr>

<tr>

<td><input type="submit name="submit" value="Add"></td>

<td><input type="reset" name="clear" value="Reset"></td>

</tr>

</table></form>

Link to comment
Share on other sites

are your sure your connect page is connecting? are you getting errors? is it saying proccess failed?  is there only 3 spots in the users table? if not you need to make blanks for each other spot..

 

also that null might have been messing it

try this(note 2 single quotes, not 1 double)

 

$myQuery = "INSERT INTO users VALUES ('', '$name', '$age')";

 

Link to comment
Share on other sites

i tried it and it makes work great. that "null" there means that hes/her primary key in the database is auto incremented the "null" and '' are d same there's nothing wrong with it.

 

also that connecting.php there i assume that chaddsuk has know it. This is the contents of that connecting.php

 

<?php

 

  DEFINE ('DB_HOST', 'localhost');

  DEFINE ('DB_USER', 'root');

  DEFINE ('DB_PASSWORD', 'password');

  DEFINE ('DB_NAME', 'database name');

 

 

  //Connect to MySQL and select the database:

 

  $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or

die ('Dili ma Connect sa MySQL! sayop ang bangag:' . mysql_error());

 

mysql_select_db(DB_NAME);

 

?>

 

and also it will say process failed if during the process it will counter an error. also it's not necessary to put a single quote in $age becoz im sure that it is an integer type not a varchar. it will cause error during the process if you put it. A variable that is assigned as a storage of the integer data inputed doesn't need to have a single quote on them except if it is a varchar type.

 

why don't you try it. so that you can see. Try to insert an integer to the database having a single quote  it will sure prompt an error.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.