Page 1 of 1

php mysql insert problem

Posted: Thu Dec 02, 2010 3:53 pm
by a-z-z
Hi all,

fairly new to this, but trying to set a page to insert into a database.

I think the problem may be that I have included both the form, and the should-be-displayed data on the same page, but I wanted to clarify before I try and change it. (the page I am quoting is insert2.php)

Following, is the problem line, when I comment this line out, the page loads fine (doesnt do anything but loads ok), but if I uncomment it, the page loads as absolutely blank:

Code: Select all

mysql_query("insert into test1 values('$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8', '$9', '$10', '$11', '$12', '$13', '$14')")
Would really appreciate any help, as can't seem to get round this.

Cheers.

Code: Select all

Hey you, add something to my base of data, I DARE you!!! <br><br>


<form name="form" action="insert2.php" method="post">

<table>

<tr><td>First Name:</td><td><input type="text" name="1" /></td></tr>
<tr><td>Surname:</td><td><input type="text" name="2" /></td></tr>
<tr><td>Address Line 1:</td><td><input type="text" name="3" /></td></tr>
<tr><td>City:</td><td><input type="text" name="4" /></td></tr>
<tr><td>County:</td><td><input type="text" name="5" /></td></tr>
<tr><td>Postcode:</td><td><input type="text" name="6" /></td></tr>
<tr><td>Telephone - Home:</td><td><input type="text" name="7" /></td></tr>
<tr><td>Telephone - Mobile:</td><td><input type="text" name="8" /></td></tr>
<tr><td>Email:</td><td><input type="text" name="9" /></td></tr>
<tr><td>Bike Manufacturer:</td><td><input type="text" name="10" /></td></tr>
<tr><td>Bike Model:</td><td><input type="text" name="11" /></td></tr>
<tr><td>Bike Colour:</td><td><input type="text" name="12" /></td></tr>
<tr><td>Bike Serial Number:</td><td><input type="text" name="13" /></td></tr>
<tr><td>ID:</td><td><input type="text" name="14" /></td></tr>

</table>

<input type="submit" name="Submit" value="Submit" />

</form>



<?php

$1 = @$_POST['1'];
$2 = @$_POST['2'];
$3 = @$_POST['3'];
$4 = @$_POST['4'];
$5 = @$_POST['5'];
$6 = @$_POST['6'];
$7 = @$_POST['7'];
$8 = @$_POST['8'];
$9 = @$_POST['9'];
$10 = @$_POST['10'];
$11 = @$_POST['11'];
$12 = @$_POST['12'];
$13 = @$_POST['13'];
$14 = @$_POST['14'];


$db_host = 'notactualhost';
$db_user = 'notactualuser';
$db_pwd = 'notactualpw';

$database = 'notactualdb';
$table = 'test1';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

mysql_query("insert into test1 values('$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8', '$9', '$10', '$11', '$12', '$13', '$14')") or die(mysql_error());
echo "inserted into db";

?>

Re: php mysql insert problem

Posted: Thu Dec 02, 2010 4:13 pm
by social_experiment
The syntax you are looking for is

Code: Select all

<?php mysql_query("INSERT INTO table (fieldname1, fieldname2) VALUES (value1, value2)"); ?>
The number of 'values' you want to insert should match the amount of fields you have (and probably vice-versa).