Help php where insertinto

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 40
Joined: Fri Aug 10, 2012 4:37 pm

Wed Aug 15, 2012 9:10 am

You don't set $code in your code, and WHERE does not work with an insert.

Code: Select all


echo "Refer to list of Profession ID's and enter details accordingly";
mysql_connect ("localhost", "root", "") or die ('Error: ' . mysql_error());
mysql_select_db ("office_db");

$name = mysql_real_escape_string( $_POST['name'] );
$code = mysql_real_escape_string( $_POST['code'] );

$query="INSERT INTO personal SET `code` = '".$code."', `name` = '".$name."';

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

echo "Database Updated";
should be fine

User avatar
php-forum Active User
php-forum Active User
Posts: 359
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Thu Aug 16, 2012 7:05 am

So you're actually doing an update. I don't think that 'set' will work with INSERT either. You probably want:

Code: Select all

$query="UPDATE personal SET `code` = '".$code."' WHERE `name` = '".$name."';

New php-forum User
New php-forum User
Posts: 35
Joined: Mon Aug 20, 2012 10:27 pm
Location: Ernakulam

Mon Aug 20, 2012 10:57 pm

Insert Data From a Form Into a Database

Here is the PHP code:

$con = mysql_connect("localhost","root","");
if (!$con)
die('Could not connect: ' . mysql_error());

mysql_select_db("my_db", $con);


$sql=mysql_query("INSERT INTO person (firstname, lastname, age)VALUES('$firstname','$lastname','$age')");//Person is the table in the database.

echo "Details added to table person successfully";

echo "Php Error";


Now we will create an HTML form that can be used to add new records to the "Persons" table.

Here is the HTML form:


<form method="post" name="insert_form">
Firstname: <input type="text" name="firstname" value=""/>
Lastname: <input type="text" name="lastname" value="" />
Age: <input type="text" name="age" value=""/>
<input type="submit" name="submit" value="submit" />


Post Reply