Help php where insertinto

Links for php scripts

Moderators: macek, egami, gesf

akki4p
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Aug 15, 2012 1:43 am

Help php where insertinto

Postby akki4p » Wed Aug 15, 2012 1:51 am

Dear Experts,

I have very little knowledge of PHP, I have been trying to cobble up a small form to insert a text value into a specific field in a specific row in a table in a database.

This is the form code:

Code: Select all

<HTML>
<form action="script.php" method="post"/>
<p>Input Name: <input type="text" name="name"</p>
//name already exists in the table
<p>Input Code: <input type="text" name="code"</p>
//need to add this value to a field called code only for that name
<input type="submit" value="Submit" />
</form>
</HTML>


This is the PHP Script:

Code: Select all

<?php
$name = $_POST['name'];
$jobs = $_POST['code'];
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");

$query="INSERT INTO personal WHERE name='".$name."' (code) VALUES ('".$code."');

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

echo "Database Updated";
?>



I understand this is a very basic syntax error, and I am not knowledgeable about it at all. Kindly help me with this and I shall be very grateful. Thank you.

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

Re: Help php where insertinto

Postby Jeroen » 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

akki4p
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Aug 15, 2012 1:43 am

Re: Help php where insertinto

Postby akki4p » Wed Aug 15, 2012 11:44 pm

Dear Sir;

Thank you very much for taking time to reply. I highly appreciate it. Please do correct me if I am wrong. The code submitted by you above also adds name into the table, right? I actually have a long table with other information in various fields. The CODE field had to be left blank at the time due to stupid office reasons. Now that people are submitting the code, I needed it to be inserted into a pre-existing table with other info.

If my sentence construction was improper above, what I mean is, I think the code, very kindly, given by you will add additional rows of data, and not post in the rows of existing data with the code field left blank, or will they detect the name already being present and add it to the code field of existing rows of names?

Kindly clarify. Thank you very much.

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

Re: Help php where insertinto

Postby freshnet » 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:

Re: Help php where insertinto

Postby Acesystech » 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>


Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 2 guests