Parse error

Can someone please tell me what my problem is here? I keep getting this error “Parse error: syntax error, unexpected ‘=’ in C:\xampp\htdocs\images\insert2.php on line 7” and just can’t figure out why.

Here is my code:[php]

<?php $path="localhost"; $username="root"; $password="xxxxxx"; $database="test"; $field1-name=$_POST['Value1']; $field2-name=$_POST['Value2']; $field3-name=$_POST['Value3']; $field4-name=$_POST['Value4']; $field5-name=$_POST['Value5']; mysql_connect($path,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO person VALUES ('','$field1-name','$field2-name','$field3-name','$field4-name','$field5-name')"; mysql_query($query); mysql_close(); ?>

[/php]

You cannot use dashes in variable names. You can use underscores though

Thanks Matt that removed the parse error now I have the following errors.

Notice: Undefined index: Value1 in C:\xampp\htdocs\images\insert2.php on line 7

Notice: Undefined index: Value2 in C:\xampp\htdocs\images\insert2.php on line 8

Notice: Undefined index: Value3 in C:\xampp\htdocs\images\insert2.php on line 9

Notice: Undefined index: Value4 in C:\xampp\htdocs\images\insert2.php on line 10

Notice: Undefined index: Value5 in C:\xampp\htdocs\images\insert2.php on line 11

Notice: Undefined variable: field1 in C:\xampp\htdocs\images\insert2.php on line 17

Notice: Undefined variable: field2 in C:\xampp\htdocs\images\insert2.php on line 17

Notice: Undefined variable: field3 in C:\xampp\htdocs\images\insert2.php on line 17

Notice: Undefined variable: field4 in C:\xampp\htdocs\images\insert2.php on line 17

Notice: Undefined variable: field5 in C:\xampp\htdocs\images\insert2.php on line 17

This would indicate your $_POST variables are missing. Can you post your form HTML?

Likely because you forgot to update the dashes in the query. So instead of $field1_name it is looking for $field1

actually I figured out what most of those previous errors were from now I just have the following,

Notice: Undefined index: Value1 in C:\xampp\htdocs\images\insert2.php on line 7

Notice: Undefined index: Value2 in C:\xampp\htdocs\images\insert2.php on line 8

Notice: Undefined index: Value3 in C:\xampp\htdocs\images\insert2.php on line 9

Notice: Undefined index: Value4 in C:\xampp\htdocs\images\insert2.php on line 10

Notice: Undefined index: Value5 in C:\xampp\htdocs\images\insert2.php on line 11

here is my html code:

<form action="insert2.php" method="post">
Value1: <input type="text" name="field1_name"><br>
Value2: <input type="text" name="field2_name"><br>
Value3: <input type="text" name="field3_name"><br>
Value4: <input type="text" name="field4_name"><br>
Value5: <input type="text" name="field5_name"><br>
<input type="Submit">
</form>

and here is the corrected php code:

[php]

<?php $path="localhost"; $username="root"; $password="xxxxx"; $database="test"; $field1_name=$_POST['Value1']; $field2_name=$_POST['Value2']; $field3_name=$_POST['Value3']; $field4_name=$_POST['Value4']; $field5_name=$_POST['Value5']; mysql_connect($path,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO person VALUES ('','$field1_name','$field2_name','$field3_name','$field4_name','$field5_name')"; mysql_query($query); mysql_close(); ?>

[/php]

Thanks for your help Matt, I figured out what the remaining problem was, made code changes to this:

[php]

<?php $path="localhost"; $username="root"; $password="xxxxx"; $database="test"; $field1=isset( $_POST['Value1']) ? $_POST['Value1'] : ""; $field2=isset( $_POST['Value2']) ? $_POST['Value2'] : ""; $field3=isset( $_POST['Value3']) ? $_POST['Value3'] : ""; $field4=isset( $_POST['Value4']) ? $_POST['Value4'] : ""; $field5=isset( $_POST['Value5']) ? $_POST['Value5'] : ""; mysql_connect($path,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO person VALUES ('','$field1','$field2','$field3','$field4','$field5')"; mysql_query($query); mysql_close(); ?>

[/php]

Now it works fine!

That is good practice but I don’t believe that fixes your problem.

Value1: <input type="text" name="field1_name"><br>
Value2: <input type="text" name="field2_name"><br>
Value3: <input type="text" name="field3_name"><br>
Value4: <input type="text" name="field4_name"><br>
Value5: <input type="text" name="field5_name"><br>

These should be named to what you are looking for in the $_POST array. e.g.

Value1: <input type="text" name="Value1"><br>
Value2: <input type="text" name="Value2"><br>
Value3: <input type="text" name="Value3"><br>
Value4: <input type="text" name="Value4"><br>
Value5: <input type="text" name="Value5"><br>

I spoke too soon, although the changes have cleared up my errors, it appears as though nothing is being posted in MySQL, all I get is a white page after submit is pressed on the input.html page. I check the tables in MySQL and they are empty and I get no errors at all. I made the changes in the html form you suggested with no change in the results. Below is what my current html and php codes is since the changes:

This is input.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Input</title>
</head>

<body>
<form action="insert2.php" method="post">
Value1: <input type="text" name="value1"></input><br/>
Value2: <input type="text" name="value2"></input><br/>
Value3: <input type="text" name="value3"></input><br/>
Value4: <input type="text" name="value4"></input><br/>
Value5: <input type="text" name="value5"></input><br/>
<input type="submit"></input>
</form></body>

</html>

This is insert2.php
[php]

<?php $path="localhost"; $username="root"; $password="xxxxx"; $database="test"; $field1=isset( $_POST['Value1']) ? $_POST['Value1'] : ""; $field2=isset( $_POST['Value2']) ? $_POST['Value2'] : ""; $field3=isset( $_POST['Value3']) ? $_POST['Value3'] : ""; $field4=isset( $_POST['Value4']) ? $_POST['Value4'] : ""; $field5=isset( $_POST['Value5']) ? $_POST['Value5'] : ""; mysql_connect($path,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO person VALUES ('','$field1','$field2','$field3','$field4','$field5')"; mysql_query($query); mysql_close(); ?>

[/php]

and this would be output.php
[php]

output.php <?php $path="localhost"; $username="root"; $password="xxxxx"; $database="test";

mysql_connect($path,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);
$query=“SELECT * FROM person”;
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>

<?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"field1"); $f2=mysql_result($result,$i,"field2"); $f3=mysql_result($result,$i,"field3"); $f4=mysql_result($result,$i,"field4"); $f5=mysql_result($result,$i,"field5"); ?> <?php $i++; } ?>
Value1 Value2 Value3 Value4 Value5
<?php echo $f1; ?> <?php echo $f2; ?> <?php echo $f3; ?> <?php echo $f4; ?> <?php echo $f5; ?>
[/php]

Just a note, I’m just a beginner with all of this and this isn’t on a production server, These are just things I’m trying to strengthen my knowledge in.

Check your case. ‘value1’ is not the same as ‘Value1’

I verified proper spelling and changed anything that was wrong but still no change.

Please post your table structure so that I can verify it matches your query.

person
Column Type Null Default Comments MIME
Value1 varchar(30) No
Value2 varchar(30) No
Value3 varchar(30) No
Value4 varchar(30) No
Value5 varchar(30) No

Right so do you see the problem here?

[php]
$f1=mysql_result($result,$i,“field1”);
$f2=mysql_result($result,$i,“field2”);
$f3=mysql_result($result,$i,“field3”);
$f4=mysql_result($result,$i,“field4”);
$f5=mysql_result($result,$i,“field5”);
[/php]

Your column names are not “field1” etc.

I figured it out Matt, in my insert2.php code on line 17 I had

(’’,’$Value1’,’$Value2’,’$Value3’,’$Value4’,’$Value5’)";

I changed it to:

(’$Value1’,’$Value2’,’$Value3’,’$Value4’,’$Value5’)";

and now the values are inserted in the database.

SQL result
Host: 127.0.0.1
Database: test
Generation Time: Apr 22, 2013 at 10:11 AM
Generated by: phpMyAdmin 3.5.2.2 / MySQL 5.5.27
SQL query: SELECT * FROM person LIMIT 0, 30 ;
Rows: 3

Value1 Value2 Value3 Value4 Value5
test test test test test
test2 test2 test2 test2 test2
test3 test3 test3 test3 test3

Yes that would have prevented your INSERT queries from working. If you add proper error_reporting you can save yourself some headaches :slight_smile:

For example, adding or die(mysql_error()) to mysql_query

[php]
$query = “INSERT INTO person VALUES
(’$field1’,’$field2’,’$field3’,’$field4’,’$field5’)”;

mysql_query($query)or die(mysql_error());
[/php]

I also recommend always specifying column names. If you modify the table in the future (add/remove a column) this query will no longer work.

For example:

[php]
$query = “INSERT INTO person (Value1,Value2,Value3,Value4,Value5) VALUES
(’$field1’,’$field2’,’$field3’,’$field4’,’$field5’)”;
[/php]

And finally, this part of your code is likely still not working if you are still using the wrong column names.

[php]
$f1=mysql_result($result,$i,“field1”);
$f2=mysql_result($result,$i,“field2”);
$f3=mysql_result($result,$i,“field3”);
$f4=mysql_result($result,$i,“field4”);
$f5=mysql_result($result,$i,“field5”);
[/php]

Thanks for you help Matt, I learned a lot from that one mistake. As for the $field portion of the code, I don’t know what to say as the script is working perfectly, I can post to the database and display from it as well. It’s ok though as this was more of a learning tool for me as I didn’t write the script I got it from a tutorial on another site and found out it was riddled with errors so I used it as a learning tool and it worked out fairly well as it’s helped me debug the scripts that I am writing. Once again, Thanks for your help, I greatly appreciate it. :smiley:

I think that there is nothing wrong with your table structure.

Sponsor our Newsletter | Privacy Policy | Terms of Service