Help php where insertinto

Links for php scripts

Moderators: egami, macek, gesf

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

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

<?php

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
freshnet
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."';

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

Mon Aug 20, 2012 10:57 pm

Insert Data From a Form Into a Database

Here is the PHP code:


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

mysql_select_db("my_db", $con);

if(isset($_REQUEST['submit']))
{
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$age=$_POST['age'];

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

if($sql)
{
echo "Details added to table person successfully";

}
else
{
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:

<html>
<body>

<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" />
</form>

</body>
</html>

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests