Solved

php syntax error, unexpected

Posted on 2013-02-05
32
283 Views
Last Modified: 2013-02-10
Hi line 28 and 33 have a issue with this script

<?php 

// Read name from form using $_POST (safest) 

$id=$_POST["name"]; 

// Connect to server 
// Replace username and password by your details 

$db = @mysql_connect("localhost","root","iqonr301"); 
if (!$db) 
{ 
        do_error("Could not connect to the server"); 
} 

// Connect to the database 
// Note that your database will be called username 

@mysql_select_db("test",$db)or do_error("Could not connect to the database"); 

//Create delete query	
$qry = "DELETE FROM teacher WHERE name='$id'";	
$result = @mysql_query($qry);		
//Check whether the query was successful or not	if
($result){       
 echo "Record: $id has been deleted.";         


} 
else 
{         
// $affected is zero so record does not exist         

do_error("No such record"); 

} 

function  do_error($error)
 {       
   echo  $error;
            die; 
} 

?> 

Open in new window

0
Comment
Question by:paddy086
  • 14
  • 10
  • 4
  • +4
32 Comments
 

Expert Comment

by:Spydie
ID: 38854391
change
$qry = "DELETE FROM teacher WHERE name='$id'";

Open in new window

to
$qry = "DELETE FROM `teacher` WHERE name='".$id."'";

Open in new window

0
 
LVL 7

Expert Comment

by:Robby Swartenbroekx
ID: 38854392
line 24/25 should be:
//Check whether the query was successful or not
if ($result) { 

Open in new window


the SQL query looks good to me
0
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38854424
There is no "if" in line 25

($result){

Open in new window

0
 

Author Comment

by:paddy086
ID: 38854514
thanks
0
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38854541
if statement started on line 22 is not closed, thats why you are getting issue
0
 

Author Comment

by:paddy086
ID: 38854553
if i do it this way i get error line 23 and 28

Notice: Array to string conversion in C:\xampp\htdocs\test2.php on line 23

Notice: Array to string conversion in C:\xampp\htdocs\test2.php on line 28
Record: Array has been deleted.

******It dose not Delete the record as it states above****
<?php 

// Read name from form using $_POST (safest) 

$id=$_POST["name"]; 

// Connect to server 
// Replace username and password by your details 

$db = @mysql_connect("localhost","root","iqonr301"); 
if (!$db) 
{ 
        do_error("Could not connect to the server"); 
} 

// Connect to the database 
// Note that your database will be called username 

@mysql_select_db("test",$db)or do_error("Could not connect to the database"); 
 
//Create delete query	

$qry = "DELETE FROM `teacher` WHERE name='".$id."'"; 
$result = @mysql_query($qry);		
//Check whether the query was successful or not	
if
($result){       
 echo "Record: $id has been deleted.";         


} 
else 
{         
// $affected is zero so record does not exist         

do_error("No such record"); 

} 

function  do_error($error)
 {       
   echo  $error;
            die; 
} 

?>

Open in new window


If i do the script this way i get a error on line 23

Notice: Array to string conversion in C:\xampp\htdocs\test2.php on line 23
No such record

******i know the record is in the SQL *********
<?php 

// Read name from form using $_POST (safest) 

$id=$_POST["name"]; 

// Connect to server 
// Replace username and password by your details 

$db = @mysql_connect("localhost","root","iqonr301"); 
if (!$db) 
{ 
        do_error("Could not connect to the server"); 
} 

// Connect to the database 
// Note that your database will be called username 

@mysql_select_db("test",$db)or do_error("Could not connect to the database"); 
 
//Create delete query	

$sql="DELETE FROM teacher WHERE name='$id'";
$result = @mysql_query($qry);		
//Check whether the query was successful or not	
if
($result){       
 echo "Record: $id has been deleted.";         


} 
else 
{         
// $affected is zero so record does not exist         

do_error("No such record"); 

} 

function  do_error($error)
 {       
   echo  $error;
            die; 
} 

?>

Open in new window

0
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38854556
Check what you are getting in $_POST["name"]; may be you are getting a array
0
 

Author Comment

by:paddy086
ID: 38854562

Open in new window

all my scripts  for this section

Html form
<html> 
<head><title>Delete Student Form</title></head> 

<body> 
<h2 align=center><font color=blue>Delete a Teacher Record</font> </h2><p> 

Enter the Name of the Teacher to be deleted:<br> 

<form action="test2.php" method="POST">  
<?php include('php.php'); ?>


DEL Teacher:  <select name="name[<?php echo $i ?>]"><?php echo $options ?></select><br>
<input type="submit" value="Delete"> 
<input type="reset" value="Reset"> 

</form> 

</body> 
</html>

Open in new window


php.php this script gets all the teacher names  in the table and displayes them in the html from drop down menu
<?php 

// Connect to server 
// Replace username and password by your details  

$db = @mysql_connect("localhost","root","iqonr301"); 
if (!$db) 
{ 
        do_error("Could not connect to the server"); 
} 


// Connect to the database 
// Note that your database will be called username 

@mysql_select_db("test",$db)or do_error("Could not connect to the database"); 

//connect to db first
$options = '';
$teachers = mysql_query('SELECT * FROM teacher ORDER BY name ASC');
while($teacher = mysql_fetch_array($teachers)) {
     $options .= sprintf("<option value='%s'>%s</option>", $teacher['name'], $teacher['name']);
}
$class_options = '';
$classes = mysql_query('SELECT * FROM class');
while($class = mysql_fetch_array($classes)) {
     $class_options .= sprintf("<option value='%s'>%s</option>", $class['name'], $class['name']);
}
$room_options = '';
$rooms = mysql_query('SELECT * FROM room');
while($room = mysql_fetch_array($rooms)) {
     $room_options .= sprintf("<option value='%s'>%s</option>", $room['number'], $room['number']);
}
$subject_options = '';
$subjects = mysql_query('SELECT * FROM subject');
while($subject = mysql_fetch_array($subjects)) {
     $subject_options .= sprintf("<option value='%s'>%s</option>", $subject['name'], $subject['name']);
}
?>

Open in new window


and the the test2.php file witch i posted in my last post
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38854565
How does your form look like ?
Isn't that a <select> ?
POST['name'] should be a single value form field.
0
 
LVL 12

Assisted Solution

by:sivagnanam chandrakanth
sivagnanam chandrakanth earned 48 total points
ID: 38854599
obviously you field is an array , see line 13 on your html form

<select name="name[<?php echo $i ?>]"><?php echo $options ?></select>

note this "name[<?php echo $i ?>]" in above line... change this to <select name="name"> if you are not using array
0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 432 total points
ID: 38854640
Get rid of all that unneeded backquote stuff, so you can read the query.
Get rid of the @ so you can see query error messages.
Add the ! to make the if statement work right (if (!$result)).
Add the debug echos to see the input value and the result of the query.



echo "id  = $id<br>";


$qry = "DELETE FROM teacher WHERE name='$id';";
$result = mysql_query($qry);

echo mysql_error();

if (!$result) {echo "query failed<br>";
}
else {echo "query worked<br>";
}

and then report the results to us.


My guess is that the query is working and your if statement is simply backwards because you forgot the ! (not) operator.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 20 total points
ID: 38854745
http://www.sitepoint.com/books/phpmysql5/

There is no need to "fly blind" on this stuff!

To the initial question, any PHP parse error that says "unexpected" means that the error occurred at or before the designated line number.  So start looking at the line in question, then gradually, line-by-line move up the script.  Look for things like mismatched quotes, parentheses, etc.  A strategy that I use to avoid having to make a large search is to write my code only a line or two at a time before I make a test.  Then when I get a parse error (and I can assure you that I get a lot more parse errors than you do) I only have a line or two to inspect.  It makes the whole programming process go much faster.
0
 

Author Comment

by:paddy086
ID: 38854957
by: sivagnanam is allready like this
DEL Teacher:  <select name="name[<?php echo $i ?>]"><?php echo $options ?></select><br>

Open in new window


by: yodercm
ERROR
Notice: Array to string conversion in C:\xampp\htdocs\test2.php on line 23
query worked
No such record

<?php 

// Read name from form using $_POST (safest) 

$id=$_POST["name"]; 

// Connect to server 
// Replace username and password by your details 

$db = @mysql_connect("localhost","root","iqonr301"); 
if (!$db) 
{ 
        do_error("Could not connect to the server"); 
} 

// Connect to the database 
// Note that your database will be called username 

@mysql_select_db("test",$db)or do_error("Could not connect to the database"); 
 


$qry = "DELETE FROM teacher WHERE name='$id';";
$result = mysql_query($qry);

echo mysql_error();

if (!$result) {echo "query failed<br>";
}
else {echo "query worked<br>";

      
// $affected is zero so record does not exist         

do_error("No such record"); 

} 

function  do_error($error)
 {       
   echo  $error;
            die; 
} 

?>

Open in new window

0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 432 total points
ID: 38855320
OK, great.  Now we know that $id is an array.  So check where $id is supposed to be created and see what it really is.

You can also use

print_r($id);

in place of the echo of $id to see what the array contains.
0
 

Author Comment

by:paddy086
ID: 38855373
We're will I put that
0
 

Author Comment

by:paddy086
ID: 38855850
yodercm

I think the issue is with my html form on line 11
i get the following error when i put in print_r($id)


******MESSAGE on internet browser**********

Notice: Array to string conversion in C:\xampp\htdocs\test2.php on line 23
query worked
Array ( [
Notice: Undefined variable: i in C:\xampp\htdocs\test2.1.php on line 11
] => ***A ) No such record


the ***A  is a test name in my database table

<html> 
<head><title>Delete Student Form</title></head> 

<body> 
<h2 align=center><font color=blue>Delete a Teacher Record</font> </h2><p> 

Enter the Name of the Teacher to be deleted:<br> 
<form action="test2.php" method="POST">  
<?php include('php.php'); ?>

DEL Teacher:  <select name="name[<?php echo $i ?>]"><?php echo $options ?></select><br>
<input type="submit" value="Delete"> 
<input type="reset" value="Reset"> 

</form> 

</body> 
</html>

Open in new window


<?php 
 
// Read name from form using $_POST (safest) 

$id=$_POST["name"]; 

// Connect to server 
// Replace username and password by your details 

$db = @mysql_connect("localhost","root","iqonr301"); 
if (!$db) 
{ 
        do_error("Could not connect to the server"); 
} 

// Connect to the database 
// Note that your database will be called username 

@mysql_select_db("test",$db)or do_error("Could not connect to the database"); 
 


$qry = "DELETE FROM teacher WHERE name='$id';";
$result = mysql_query($qry);

echo mysql_error();

if (!$result) {echo "query failed<br>";
}
else {echo "query worked<br>";

print_r($id);      
// $affected is zero so record does not exist         

do_error("No such record"); 

} 

function  do_error($error)
 {       
   echo  $error;
            die; 
} 

?>

Open in new window

0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 432 total points
ID: 38856098
ok that looks like where you are setting the field that eventually becomes $id is wrong.
0
 

Author Comment

by:paddy086
ID: 38856121
yodercm

any clues what to change or do
0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 432 total points
ID: 38856127
Try this.  Replace this line:

DEL Teacher:  <select name="name[<?php echo $i ?>]"><?php echo $options ?></select><br>



with this one:

DEL Teacher:  <input name="name" type=text value="testname"><br>


You can replace "testname" with whatever you want.   See if this works.
0
 

Author Comment

by:paddy086
ID: 38856146
i get the following error but it dose delete from my database table test1 has been removed

query worked
test1 No such record

but this change makes a text input field instead of a dropdown box that pulls from a table using php
0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 432 total points
ID: 38856188
ok, that much is settled.  

Now if you want to populate a <select input with data from the table, you have to add php to the html page to retrieve the values from the database, and put them into the html.

First set up the <form and <select name="name" ...> part of the html.

Then switch to php and

1.  Connect to your database and query the table to get the teacher names.
2.  Loop through the query results and put each name into an option line.  This example uses $teacher, but that value will actually come from the query output

echo "<option value='$teacher' >";

Then when you have done all the teacher names, go back to html and finish with the </select> and whatever else you need.
0
 

Author Comment

by:paddy086
ID: 38856221
but thats what this line did
DEL Teacher:  <select name="name[<?php echo $i ?>]"><?php echo $options ?></select><br>

Open in new window


it uses a php script called php.php to get the info from the database and dispaly it in the dropdown bar

html form whit php
<html> 
<head><title>Delete Student Form</title></head> 

<body> 
<h2 align=center><font color=blue>Delete a Teacher Record</font> </h2><p> 

Enter the Name of the Teacher to be deleted:<br> 
<form action="test2.php" method="POST">  
<?php include('php.php'); ?>

DEL Teacher:  <select name="name[<?php echo $i ?>]"><?php echo $options ?></select><br>

<input type="submit" value="Delete"> 
<input type="reset" value="Reset"> 

</form> 

</body> 
</html>

Open in new window



php.php
<?php 

// Connect to server 
// Replace username and password by your details  

$db = @mysql_connect("localhost","root","iqonr301"); 
if (!$db) 
{ 
        do_error("Could not connect to the server"); 
} 


// Connect to the database 
// Note that your database will be called username 

@mysql_select_db("test",$db)or do_error("Could not connect to the database"); 

//connect to db first
$options = '';
$teachers = mysql_query('SELECT * FROM teacher ORDER BY name ASC');
while($teacher = mysql_fetch_array($teachers)) {
     $options .= sprintf("<option value='%s'>%s</option>", $teacher['name'], $teacher['name']);
}
$class_options = '';
$classes = mysql_query('SELECT * FROM class');
while($class = mysql_fetch_array($classes)) {
     $class_options .= sprintf("<option value='%s'>%s</option>", $class['name'], $class['name']);
}
$room_options = '';
$rooms = mysql_query('SELECT * FROM room');
while($room = mysql_fetch_array($rooms)) {
     $room_options .= sprintf("<option value='%s'>%s</option>", $room['number'], $room['number']);
}
$subject_options = '';
$subjects = mysql_query('SELECT * FROM subject');
while($subject = mysql_fetch_array($subjects)) {
     $subject_options .= sprintf("<option value='%s'>%s</option>", $subject['name'], $subject['name']);
}
?>

Open in new window

0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 432 total points
ID: 38856244
OK, I see what you are doing.  You need to move the php.php after the <select since you are creating the <option statements in it.

Eventual HTML has to be

<select
<option
<option
</select
0
 

Author Comment

by:paddy086
ID: 38856256
sorry yodercm what do i do? can you show me in my script thanks
0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 432 total points
ID: 38856375
Start with changing this:

<form action="test2.php" method="POST">  
<?php include('php.php'); ?>

DEL Teacher:  <select name="name[<?php echo $i ?>]"><?php echo $options ?></select><br>


to this:

<form action="test2.php" method="POST">  

DEL Teacher:  <select name="name">
<?php include('php.php'); ?>

</select><br>

Then if your php.php is working correctly, you should see the dropdown list of names.

By the way, this sounds a little bit like a class assignment for a course, is it?
0
 

Author Comment

by:paddy086
ID: 38856702
HI yodercm
no this is not a school project its for work its a sick teacher form if a teacher is sick we have to show who is covering. so far i have a website running local in the school that dose this but we get new teacher in and old teacher leave the school so the data base of names gets big so i am creating a delete page on the website.....

my script works on the html page as it should it shows up the names in a drop down menu from a database called (test) table called (teacher) row called (name)
i have attached image 1 to show my script on the HTML form using php.php script to get the names from the database to the HTML FORM.....

<form action="test2.php" method="POST">  
<?php include('php.php'); ?>

DEL Teacher:  <select name="name[<?php echo $i ?>]"><?php echo $options ?></select><br>

Open in new window


HTML Form shows the teachers names in dropdown menu

*****Script called test2.1.php

<html> 
<head><title>Delete Student Form</title></head> 

<body> 
<h2 align=center><font color=blue>Delete a Teacher Record</font> </h2><p> 

Enter the Name of the Teacher to be deleted:<br> 
<form action="test2.php" method="POST">  
<?php include('php.php'); ?>

DEL Teacher:  <select name="name[<?php echo $i ?>]"><?php echo $options ?></select><br>

<input type="submit" value="Delete"> 
<input type="reset" value="Reset"> 

</form> 

</body> 
</html>

Open in new window


*****Script called php.php

php script looks to the DATABASE test and gets the names of the teachers lines 18 to 22 deal with teachers names
<?php 

// Connect to server 
// Replace username and password by your details  

$db = @mysql_connect("localhost","root","iqonr301"); 
if (!$db) 
{ 
        do_error("Could not connect to the server"); 
} 


// Connect to the database 
// Note that your database will be called username 

@mysql_select_db("test",$db)or do_error("Could not connect to the database"); 

//connect to db first
$options = '';
$teachers = mysql_query('SELECT * FROM teacher ORDER BY name ASC');
while($teacher = mysql_fetch_array($teachers)) {
     $options .= sprintf("<option value='%s'>%s</option>", $teacher['name'], $teacher['name']);
}
$class_options = '';
$classes = mysql_query('SELECT * FROM class');
while($class = mysql_fetch_array($classes)) {
     $class_options .= sprintf("<option value='%s'>%s</option>", $class['name'], $class['name']);
}
$room_options = '';
$rooms = mysql_query('SELECT * FROM room');
while($room = mysql_fetch_array($rooms)) {
     $room_options .= sprintf("<option value='%s'>%s</option>", $room['number'], $room['number']);
}
$subject_options = '';
$subjects = mysql_query('SELECT * FROM subject');
while($subject = mysql_fetch_array($subjects)) {
     $subject_options .= sprintf("<option value='%s'>%s</option>", $subject['name'], $subject['name']);
}
?>

Open in new window


once the user selects the teacher name for deleting the HTML FORM then uses test2.php script to carry out the deleting of that teachers name.....

******PROBLEM SECTION*******

BUT this is where the issue happens im not sure if test2.php is getting the name form the form correctly as if i just make a text box on the HTML FORM page and manually type in a teacher name well then it deletes the name from the database.

but if i use the drop down menu it like test2.php cant see or read the name to be deleted...
i hope i am making sense with all this

use the name to

*****Script called  test2.php  *********
<?php 
 
// Read name from form using $_POST (safest) 

$id=$_POST["name"]; 

// Connect to server 
// Replace username and password by your details 

$db = @mysql_connect("localhost","root","iqonr301"); 
if (!$db) 
{ 
        do_error("Could not connect to the server"); 
} 

// Connect to the database 
// Note that your database will be called username 

@mysql_select_db("test",$db)or do_error("Could not connect to the database"); 
 


$qry = "DELETE FROM teacher WHERE name='$id';";
$result = mysql_query($qry);

echo mysql_error();

if (!$result) {echo "query failed<br>";
}
else {echo "query worked<br>";

print_r($id);      
// $affected is zero so record does not exist         

do_error("No such record"); 

} 

function  do_error($error)
 {       
   echo  $error;
            die; 
} 

?>

Open in new window


and this is the following error i get from this script
Notice: Array to string conversion in C:\xampp\htdocs\test2.php on line 23
query worked
Array ( [
Notice: Undefined variable: i in C:\xampp\htdocs\test2.1.php on line 11
] => ******** ) No such record
image-1.png
0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 432 total points
ID: 38857058
Right after

$id = $_POST["name"];

put

echo "<br>id = $id<br>";
print_r($id);


That will show you what you are getting from the form.
0
 

Author Comment

by:paddy086
ID: 38857112
Notice: Array to string conversion in C:\xampp\htdocs\test2.php on line 7


id = Array
Array ( [
Notice: Undefined variable: i in C:\xampp\htdocs\test2.1.php on line 12
] => ****aaaaaa )
Notice: Array to string conversion in C:\xampp\htdocs\test2.php on line 26
query worked
Array ( [
Notice: Undefined variable: i in C:\xampp\htdocs\test2.1.php on line 12
] => ****aaaaaa ) No such record
0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 432 total points
ID: 38857372
ok, so the echo shows that $id is actually an array, and print_r shows you the contents of that array, which is contains one element, "****aaaaaa".

It looks like you haven't fixed the select section in test2.1.php like I gave you.   See the post by: yodercm Posted on 2013-02-05 at 13:20:28 ID: 38856375 above.

It still seems to have the incorrect <select ... line.
0
 

Accepted Solution

by:
paddy086 earned 0 total points
ID: 38857402
The Following line fixed my issue
<select name="name"><?php echo $options ?></select><br>

i had this <select name="name[<?php echo $i ?>]">
0
 
LVL 27

Expert Comment

by:yodercm
ID: 38857815
Glad you sorted it out.  

By the way, I'm "her", not "him" :)
0
 

Author Closing Comment

by:paddy086
ID: 38872817
yodercm was well on his way to give me the answer and I could not have done any of this without his help I gave myself the best answer as my answer fixed my problem but only with the help of yodercm

SO THANK YOU yodercm
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now