DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

Need help with insert

By: rekha singh | 13 Mar 2010 12:36 pm

I could not figure out what went wrong. I am trying to use INSERT. Here is my code below. I tried different way like ".$first_name. " or
"$_POST[first_ name]"
Reid

editmissing. php

<TITLE> Update Missing </TITLE>
</HEAD>
<BODY>
<form method="post" action="updatemissi ng.php">

First Name:<br/>
<input type="text" name="first_ name" size="30" /><br/>

Maiden Name:<br/>
<input type="text" name="maiden_ name" size="30" /><br/>

Last Name:<br/>
<input type="text" name="last_name" size="30" /><br/>

Month Start:<br/>
<input type="text" name="month_ start" size="30" /><br/>

Year Start:<br/>
<input type="text" name="year_start" size="30" /><br/>

Month End:<br/>
<input type="text" name="month_ end" size="30" /><br/>

Year End:<br/>
<input type="text" name="year_end" size="30" /><br/>

<input type="submit" value="Update Database" />

</form>
</BODY>
</HTML>

updatemissing. php

//Database Information
$dbhost = "xxxx";
$dbname = "xxxx";
$dbuser = "xxxx";
$dbpass = "xxxx";

$link = mysql_connect ($dbhost, $dbuser, $dbpass)or die("Could not connection. ");

$db = mysql_select_ db($dbname, $link) or die("Couldn' t select database");

$first_name = $_POST['first_ name'];
$maiden_name = $_POST['maiden_ name'];
$last_name = $_POST['last_ name'];
$month_start = $_POST['month_ start'];
$year_start = $_POST['year_ start'];
$month_end = $_POST['month_ end'];
$year_end = $_POST['year_ end'];

$query="INSERT INTO missing (first_name, maiden_name, last_name, month_start, year_start, month_end, year_end) 
VALUES(".$first_ name.",". $maiden_name. ",".$last_ name.",". $month_start. ",".$year_ start."," .$month_end. ",".$year_ end.")";

mysql_query( $query) or die ('Error updating database');

echo "Database Updated With: " .$first_name. " ".$maiden_name. " ".$last_name. " ".$month_start. " ".$year_start. " ".$month_end. " ".$year_end ;

?>

Comments

Just a little tip: post the exact nature of what is going wrong (error code, etc) instead of just saying it doesn't work. 90% of the time the error points directly to the problem.

Anyway, look in your query and try placing all four database variables in "mysql_connect( )"

Also make sure there isn't a space in the target URL for your form's action tag. [action="updatemiss i ng.php"] should be [action="updatemiss ing.php"] .

--------- Example -----------
$link = mysql_connect ($dbhost, $dbuser, $dbpass, $dbname)or die("Could not connection. ");
------------ --------- --------- --

Then, get rid of "mysql_select_ db" altogether unless you really need it.

I would also suggest using the mysqli_real_ escape_string for all insertions and closing the connection when the insertion is done using "mysqli_close( $cxn);"

I also make use of "trim" in all my insertions just to make it clean.

Here is the INSERT syntax of a current in-use script:

--------- Example -----------
<?php
$host = "hostHere";
$user = "userHere";
$password = "passwordHere" ;
$database = "dbNameHere" ;
$cxn = mysqli_connect( $host,$user, $password, $database) or die ("Cannot connect to $database, see system administrator. ");

$var1 = mysqli_real_ escape_string( $cxn, trim($yourVariable) );
$var2 = mysqli_real_ escape_string( $cxn, trim($yourVariable2 ));
$var3 = mysqli_real_ escape_string( $cxn, trim($yourVariable3 ));

$query = "INSERT INTO tableName (field1, field2, field3) VALUE ('var1','$var2' ,'$var3') ";
$result = mysqli_query( $cxn,$query) or die ("Could not execute query, see system administrator. ");

mysqli_close( $cxn);
?>

By: rekha singh | 13 Mar 2010

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move