• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

php syntax error, unexpected

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
paddy086
Asked:
paddy086
  • 14
  • 10
  • 4
  • +4
12 Solutions
 
SpydieCommented:
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
 
Robby SwartenbroekxMSP engineerCommented:
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
 
sivagnanam chandrakanthTechnical LeadCommented:
There is no "if" in line 25

($result){

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
paddy086Author Commented:
thanks
0
 
sivagnanam chandrakanthTechnical LeadCommented:
if statement started on line 22 is not closed, thats why you are getting issue
0
 
paddy086Author Commented:
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
 
sivagnanam chandrakanthTechnical LeadCommented:
Check what you are getting in $_POST["name"]; may be you are getting a array
0
 
paddy086Author Commented:

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
 
Lukasz ChmielewskiCommented:
How does your form look like ?
Isn't that a <select> ?
POST['name'] should be a single value form field.
0
 
sivagnanam chandrakanthTechnical LeadCommented:
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
 
Cornelia YoderArtistCommented:
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
 
Ray PaseurCommented:
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
 
paddy086Author Commented:
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
 
Cornelia YoderArtistCommented:
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
 
paddy086Author Commented:
We're will I put that
0
 
paddy086Author Commented:
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
 
Cornelia YoderArtistCommented:
ok that looks like where you are setting the field that eventually becomes $id is wrong.
0
 
paddy086Author Commented:
yodercm

any clues what to change or do
0
 
Cornelia YoderArtistCommented:
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
 
paddy086Author Commented:
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
 
Cornelia YoderArtistCommented:
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
 
paddy086Author Commented:
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
 
Cornelia YoderArtistCommented:
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
 
paddy086Author Commented:
sorry yodercm what do i do? can you show me in my script thanks
0
 
Cornelia YoderArtistCommented:
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
 
paddy086Author Commented:
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
 
Cornelia YoderArtistCommented:
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
 
paddy086Author Commented:
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
 
Cornelia YoderArtistCommented:
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
 
paddy086Author Commented:
The Following line fixed my issue
<select name="name"><?php echo $options ?></select><br>

i had this <select name="name[<?php echo $i ?>]">
0
 
Cornelia YoderArtistCommented:
Glad you sorted it out.  

By the way, I'm "her", not "him" :)
0
 
paddy086Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 14
  • 10
  • 4
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now