Parse error: syntax error, unexpected T_INC,
Hi Folks,
I am having real trouble with my code. Could somebody have a look at it for me to see what I am doing wrong please? The code is pasted below.
Code:
<html>
<head>
<title>B&B cars</title>
</head>
<body>
<h1>Boys and Babes cars</h1>
<?php
//Some short variable declared
$searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);
if (!$searchtype || !$searchterm) {
echo 'You did not enter any search term.';
exit;
}
if (!get_magic_quotes_gpc()) {
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
@ $db = new mysqli('localhost', 'root', '', 'cars');
if (mysqli_connect_error()) {
echo 'Error: Was not able to connect.';
exit;
}
$query = "select * from cars where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
echo '<p>Number of cars found: ".$num_results."</p>';
for ($i=0; $i < $num_results; $++) {
$row = $result->fetch_assoc();
echo "<p><strong>". ($i+1).". Car: ";
echo htmlspecialchars(stripslashes($row['car']));
echo "</strong>"<br />Make: ";
echo stripslahes($row['make']);
echo "<br />Year: ";
echo stripslashes($row['year']);
echo "<br />Price: ";
echo stripslashes($row['price')];
echo "<p />";
}
$result->free();
$db->close();
?>
</body>
</html>
When I run the code above, I get the error message below,
Code:
Parse error: syntax error, unexpected T_INC, expecting T_VARIABLE or '$' in C:\xampp\htdocs\menre\products\cars\results.php on line 35.
I will welcome your help please.
Thanks,
Menre
Re: Parse error: syntax error, unexpected T_INC,
Your quotes are mismatched on line 35 and you have an extra double quote on line 41.
Code:
echo '<p>Number of cars found: ".$num_results."</p>';
Code:
echo "</strong>"<br />Make: ";
You should be able to find these quickly with a syntax-highlighting editor such as vim or Notepad++.
Re: Parse error: syntax error, unexpected T_INC,...
Thank you for your response. I have made those corrections, but the error message is still the same.
Code:
Parse error: syntax error, unexpected T_INC, expecting T_VARIABLE or '$' in C:\xampp\htdocs\menre\products\cars\results.php on line 35.
could the error message actually be coming from the line above it?
Code:
$num_results = $result->num_rows;
Thanks
Menre
Re: Parse error: syntax error, unexpected T_INC,
unless you post the code that you've changed, no one will really be able to help you. we can't see what you did, or did not, change.
Re: Parse error: syntax error, unexpected T_INC,
This is the code that I changed based on earlier suggestions.
Code:
<html>
<head>
<title>B&B cars</title>
</head>
<body>
<h1>Boys and Babes cars</h1>
<?php
//Some short variable declared
$searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);
if (!$searchtype || !$searchterm) {
echo 'You did not enter any search term.';
exit;
}
if (!get_magic_quotes_gpc()) {
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
@ $db = new mysqli('localhost', 'root', '', 'cars');
if (mysqli_connect_error()) {
echo 'Error: Was not able to connect.';
exit;
}
$query = "select * from cars where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of cars found: ".$num_results."</p>";
for ($i=0; $i < $num_results; $++) {
$row = $result->fetch_assoc();
echo "<p><strong>". ($i+1).". Car: ";
echo htmlspecialchars(stripslashes($row['car']));
echo "</strong><br />Make: ";
echo stripslahes($row['make']);
echo "<br />Year: ";
echo stripslashes($row['year']);
echo "<br />Price: ";
echo stripslashes($row['price')];
echo "<p />";
}
$result->free();
$db->close();
?>
</body>
</html>
Whenever I run it, I get the error message below.
Code:
Parse error: syntax error, unexpected T_INC, expecting T_VARIABLE or '$' in C:\xampp\htdocs\menre\products\cars\results.php on line 35.
I still cannot understand what I am doing wrong. I will appreciate some help please.
Re: Parse error: syntax error, unexpected T_INC,
I don't know how you got line 35. The error is on line 37, which is this one.
Code:
for ($i=0; $i < $num_results; $++) {
Spot the missing variable name.
Re: Parse error: syntax error, unexpected T_INC,
Oh, thanks for sporting the error on that line. I have now added the right variable '$i++' to line 37. But another issues has come up. after I have made the corrections, when I run the page I get the error message below:
Code:
Notice: Undefined index: searchtype in C:\xampp\htdocs\menre\products\cars\results.php on line 9
Notice: Undefined index: searchterm in C:\xampp\htdocs\menre\products\cars\results.php on line 10
You have not entered search details. Please go back and try again.
The file for the results.php is attached to a form and when I search for something in that form, I get the eror message below:
Code:
Notice: Trying to get property of non-object in C:\xampp\htdocs\menre\products\cars\results.php on line 33
Number of cars found:
Fatal error: Call to a member function free() on a non-object in C:\xampp\htdocs\menre\products\cars\results.php on line 49
I will appreciate further help please.
Thanks.
Re: Parse error: syntax error, unexpected T_INC,
"Undefined index" errors mean you are trying to access an index in an array that doesn't exist: $_POST['searchtype'] and $_POST['searchterm']. So check if they exist before trying to use them:
PHP Code:
if(isset($_POST['searchtype'])){
$searchtype=$_POST['searchtype'];
}
Your other error ("Trying to get property of non-object") suggests that something went wrong with your query. Have your script echo out the query and see if it makes sense. You can also add a check for failure:
PHP Code:
$result = $db->query($query);
if($result){
//put your code here that runs only if query was successful
}else{
//your query failed
}
Re: Parse error: syntax error, unexpected T_INC,
Hello Folks,
Thanks for your responses and suggestions tomy problems. I have been trying to solve the problem on my own but it is not over yet and I will appreciate further help please.
The code that I am using for both files are pasted below.
Code:
<html>
<head>
<title>Boys and Babes cars</title>
</head>
<body>
<h2>Boys and Babes cars</h2>
<form action="results.php" method="post">
Choose Search Type:<br />
<select name="searchtype">
<option value="car">Car</option>
<option value="make">Make</option>
<option value="year">Year</option>
<option value="price">Price</option>
</select>
<br />
Enter Search Term <br />
<input name="searchterm" type="text" size="40"/> <br />
<input type="submit" name="submit" value="Search"/>
</form>
</body>
</html>
Code:
<html>
<head>
<title>B&B cars</title>
</head>
<body>
<h1>Boys and Babes cars</h1>
<?php
//Some short variable declared
$searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);
if (!$searchtype || !$searchterm) {
echo 'You did not enter any search term.';
exit;
}
if (!get_magic_quotes_gpc()) {
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
@ $db = new mysqli('localhost', 'root', '', 'cars');
if (mysqli_connect_error()) {
echo 'Error: Was not able to connect.';
exit;
}
$query = "select * from cars where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of cars found: ".$num_results."</p>";
for ($i=0; $i < $num_results; $++) {
$row = $result->fetch_assoc();
echo "<p><strong>". ($i+1).". Car: ";
echo htmlspecialchars(stripslashes($row['car']));
echo "</strong><br />Make: ";
echo stripslahes($row['make']);
echo "<br />Year: ";
echo stripslashes($row['year']);
echo "<br />Price: ";
echo stripslashes($row['price')];
echo "<p />";
}
$result->free();
$db->close();
?>
</body>
</html>
When i run both code above, I get the error messages below.
Code:
Notice: Undefined index: searchtype in C:\xampp\htdocs\menre\products\cars\results.php on line 9
Notice: Undefined index: searchterm in C:\xampp\htdocs\menre\products\cars\results.php on line 10
You have not entered search details. Please go back and try again.
Code:
Notice: Trying to get property of non-object in C:\xampp\htdocs\menre\products\cars\results.php on line 33
Number of cars found:
Fatal error: Call to a member function free() on a non-object in C:\xampp\htdocs\menre\products\cars\results.php on line 49
I still do not know what I am doing wrong. I will really appreciate your help please.
Menre
Re: Parse error: syntax error, unexpected T_INC,
the two notices in your first quote are probably because on the initial page load, your $_POST variables are not set. thus, both $_POST['searchtype'] and $_POST['searchterm'] are undefined. you can get around this by using a conditional statement to either set them if they exist, or otherwise give them a default value:
PHP Code:
$searchType = post("searchtype");
$searchTerm = post("searchterm");
function post($key, $default = ''){
return isset($_POST[$key]) ? $_POST[$key] : $default;
}
this would fix your first set of notices.
later on, you're trying to access an object's property on line 33 ($result->num_rows). $result, in this case, is not an object, which probably means that when you tried to set it (line 31), your query failed. you should be able to place your query in a conditional statement:
PHP Code:
if($result = $db->query($query)){
// Query succeeded
}else{
// Query failed
echo $db->errno . ": " . $db->error . "<br />" . $query;
}
If you use the above code and it fails, you'll at least be printing out the error message along with the SQL statement that failed to execute. you can check the syntax/run it manually in phpMyAdmin to see what's actually wrong with it, if the error message isn't helpful.
also, one of the real benefits to using a library like MySQLi or PDO is to be able to use prepared statements. however, you're not utilizing them in this case (and instead are manually [and wrongly] escaping the variables you're using in your query). I'd read up on how to use them, if I were you.