Jump to content

PHP HELP ??


lill77

Recommended Posts

Hello i'm trying to teach myself PHP

 

I have the following error code when trying to add a product into mysql database form new-product.php

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') VALUES ( '', '', 'size', '' , 'noimage.png', 'noimage_big.png', )' at line 1

 

Any ideas please my code is below??

Thanks  :D

 

<?php

include 'db.php';

// open connection

$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

 

// select database

mysql_select_db($db) or die ("Unable to select database!");

$sql = "INSERT INTO `products` (`category_id` , `title` , `size` , `description` , `image` , `imageenlarge` ,)

VALUES ( '', '', 'size', '' , 'noimage.png', 'noimage_big.png', );";

 

$sql_query = mysql_query($sql) or die (mysql_error());

 

$sql = "SELECT LAST_INSERT_ID()"; 

$result = mysql_query($sql);

$row = mysql_fetch_row($result);

$product_id = $row[0];

 

// free result set memory

mysql_free_result($result);

 

// close connection

mysql_close($connection);

 

?>

 

<META HTTP-EQUIV=Refresh CONTENT='0; URL=index.php?page=edit-product.php&product=<? echo $product_id; ?>'>

<p align="center" class="smalltext style2 style1">Saving Changes</p>

Link to comment
Share on other sites

first, next time you post something, please try to use a more descriptive title.

second, when posting code, use code tags. it's the button on the toolbar with the # symbol

 

That error is from the extra comas in the SQL query. Try this instead:

$sql = "INSERT INTO `products` (`category_id` , `title` , `size` , `description` , `image` , `imageenlarge`)
VALUES ( '', '', 'size', '' , 'noimage.png', 'noimage_big.png');";

Link to comment
Share on other sites

the capital P shouldn't matter, but i would try with all lower case just to make sure.

 

the following won't work...you can't have an empty field like that in the list of fields to insert into.

 

maybe you can try this:

 

$sql = "INSERT INTO `products` (``, `category_id` , `title` , `size` , `description` , `image` , `imageenlarge`)
VALUES ('', '', '', 'size', '' , 'noimage.png', 'noimage_big.png');";

 

 

Link to comment
Share on other sites

Sorry am new to this here you go ?

 

Thanks

 

 

<?php

include 'db.php';

// open connection

$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

 

// select database

mysql_select_db($db) or die ("Unable to select database!");

 

$sql = "INSERT INTO `products` (``, `category_id` , `title` , `size` , `description` , `image` , `imageenlarge`)

VALUES ( '' , '', '', 'size', '' , 'noimage.png', 'noimage_big.png');";

 

$sql_query = mysql_query($sql) or die (mysql_error());

 

$sql = "SELECT LAST_INSERT_ID()"; 

$result = mysql_query($sql);

$row = mysql_fetch_row($result);

$product_id = $row[0];

 

// free result set memory

mysql_free_result($result);

 

// close connection

mysql_close($connection);

 

?>

 

<META HTTP-EQUIV=Refresh CONTENT='0; URL=index.php?page=edit-product.php&product=<? echo $product_id; ?>'>

<p align="center" class="smalltext style2 style1">Saving Changes</p>

 

Link to comment
Share on other sites

again, please use CODE tags

 

try this:

<?php
include 'db.php';
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

$sql = "INSERT INTO `products` (`category_id` , `title` , `size` , `description` , `image` , `imageenlarge`)
VALUES ( '', '', 'size', '' , 'noimage.png', 'noimage_big.png');";

$reult = mysql_query($sql) or die (mysql_error());
$product_id = mysql_insert_id();

// close connection
mysql_close($connection);

?>

<META HTTP-EQUIV=Refresh CONTENT='0; URL=index.php?page=edit-product.php&product=<? echo $product_id; ?>'>
<p align="center" class="smalltext style2 style1">Saving Changes</p>

Link to comment
Share on other sites

Hello getting this error

 

Error in query: SELECT * FROM `Products` WHERE `ID` = '3'. Table 'characte_characterworld.Products' doesn't exist

 

Could it because of the code on my products.php page as below

 

<? include 'db.php' ?>
  
  
  
  <?php

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

if ($sort ==""){
$sort = "title"; 
}
// create query
$query = "SELECT * FROM products, category WHERE category_id = category.ID ORDER BY $sort ASC";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
    echo "<table align =center border=1 width=800 id=table1 cellspacing=2 cellpadding=2 style=border-collapse: collapse >";
    echo "<tr>";
    echo "<td bgcolor='#6699FF'><b><font face='Arial' color='#FFFFFF'> <a href='index.php?sort=productID'>ID</a> </font></b></td>";
echo "<td bgcolor='#6699FF'><b><font face='Arial' color='#FFFFFF'> <a href='index.php?sort=category_id'> Category </a></font></b></td>";
echo "<td bgcolor='#6699FF'><b><font face='Arial' color='#FFFFFF'> <a href='index.php?sort=title'> title </a></font></b></td>";
echo "<td bgcolor='#6699FF'><b><font face='Arial' color='#FFFFFF'> <a href='index.php?sort=size'> size </a></font></b></td>";
echo "<td bgcolor='#6699FF'><b><font face='Arial' color='#FFFFFF'> <a href='index.php?sort=description'> description </a> </font></b></td>";
echo "<td bgcolor='#6699FF'><b><font face='Arial' color='#FFFFFF'></font></b></td>";
echo "<td bgcolor='#6699FF'><b><font face='Arial' color='#FFFFFF'></font></b></td>";


echo "</tr>";

    while($row = mysql_fetch_row($result)) {

        echo "<tr border=0>";
	echo "<td>".$row[0]."</td>"; // id
	echo "<td>".$row[2]."</td>"; // title
	echo "<td>".$row[1]."</td>"; // title
	echo "<td>".$row[3]."</td>"; // size
  		echo "<td>".substr($row[3], 0, 30)."...</td>"; // description
	echo "<td>".$row[7]."</td>"; //
	echo "<td><a href='?page=edit-product.php&product=$row[0]'>Edit</a></td>"; //
	echo "<td align = 'centre'><a href='delete-product.php?id=$row[0]' onclick='return log_out()'>Delete</a></td>";
       
        echo "</tr>";
    }
    echo "</table>";
}
else {
    // no
    // print status message
    echo "<p align='center'><font color='#808080' face='Arial'>No records found.</font></p>";
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>

Link to comment
Share on other sites

Here you go, yes i think so !!

 

<!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 http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<? include 'db.php' ?>
<script language="javascript" type="text/javascript"><!--
function popupWindow(url, width, height) {
if(width == null){	width =450; }	
if(height == null){height =150;}

  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',screenX=150,screenY=150,top=150,left=100')
}
//--></script>
<script>
function log_out()
{
return confirm('Are you sure you want to Delete this article?');
}
</script>

<?php

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");




// create query
$query = "SELECT * FROM `Products` WHERE `ID` = '".$_REQUEST['product']."'";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
$row = mysql_fetch_row($result);
$CurrentProduct = $row[0];

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);
?>
<body>
<form method="GET" action="update-product.php">
<div align="center">
<table width="505" border="0" cellpadding="2" cellspacing="2" bordercolor="#FFFFFF" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0" id="table1">
	<tr>
		<td width="130" align="right"><b>
		<font face="Arial" size="2" color="#808080">Product ID:</font></b></td>
		<td width="228" align="center">
		  <p align="left">
		  	<? echo "$row[0]" ?>
		    <input name="ID" type="hidden" id="ID" value="<? echo "$row[0]" ?>" />
		  </p>
	  </td>
		<td width="127" align="right"> </td>
	</tr>
	<tr>
		<td width="130" align="right"><b>
		<font face="Arial" size="2" color="#808080">Title:</font></b></td>
		<td align="center">
		  <div align="left">
		    <input type="text" name="title" size="36" value="<? echo "$row[2]" ?>" ?>
        </div></td>
		<td width="127" align="right"> </td>
        </tr>
	<tr>
		<td width="130"><div align="right"><b><font face="Arial" size="2" color="#808080">Sizes </font></b></div></td>
		<td align="center"><div align="left">
		  <input type="text" name="size" size="36" value="<? echo "$row[3]" ?>" ? /><br />
		  (Format: <b>Small/Medium/Large</b>)
		</div></td>
		<td width="127"> </td>
	</tr>
	<tr>
		<td width="130" align="right"><b><font face="Arial" size="2" color="#808080">Description:</font></b></td>
	  <td align="center"><div align="left">
	    <textarea name="description" cols="50" rows="10" id="description"><? echo "$row[4]" ?></textarea>
      </div></td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130" align="right"><b><font face="Arial" size="2" color="#808080">Category:</font></b></td>
		<td align="center"><div align="left">
		  <p>			    		      </p>
		  <p><select name="category_id" id="category_id">
      <?php
$currentcat = $row[4];

// set database server access variables:

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "SELECT * FROM `Category`;";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
        

    
    while($row = mysql_fetch_row($result)) {
        echo "<option value=\"".$row[0]."\"";
	if ($row[0]==$currentcat) {echo " Selected";};
	echo ">$row[1]</option>";
        
    }

}


// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>
    </select>   </p>
		</div></td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130" align="right"><b><font face="Arial" size="2" color="#808080">Image:</font></b></td>
	    <td align="center"><div align="left">
	      <select name="image" id="image">
	        <option value="noimage.png">No Image</option>
            <?php
if ($handle = opendir('../productimages/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
	$page_name = $file;
            //$page_name=substr($file, 0, strpos($file, "."));
            echo "<option";
		if($page_name==$row[4]){echo ' selected';};
		echo ">$page_name</option>";
        }
    }
}
closedir($handle);
?>
              </select>
            </div></td>

	    <td width="127" align="right"> </td>
    </tr>
	<tr>
	  <td align="right"><b><font face="Arial" size="2" color="#808080">Enlarge Image:</font></b></td>
	  <td align="center"><div align="left">
	    <select name="imageenlarge" id="imageenlarge">
	      <option value="noimage.png">No Image</option>
	      <?php
if ($handle = opendir('../productimages/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
	$page_name = $file;
            //$page_name=substr($file, 0, strpos($file, "."));
            echo "<option";
		if($page_name==$row[5]){echo ' selected';};
		echo ">$page_name</option>";
        }
    }
}
closedir($handle);
?>
        </select>
      </div></td>
	  <td align="right"> </td>
  </tr>
	<tr>
		<td width="130" align="right"> </td>
    <td align="center"><div align="left"></div></td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130"> </td>
		<td align="center"><div align="left"></div></td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130" align="right"> </td>
	  <td align="center"> </td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130" align="right"> </td>
	  <td align="center"> </td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
	  <td width="130"> </td>
		<td align="center"> </td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
	  <td> </td>
	  <td align="center"> </td>
	  <td> </td>
  </tr>
  </table>
</div>
<p align="center"><input type="submit" value="Save" name="B1">
  <input type="reset" value="Close" name="B2" onclick="history.back()">
</p>
</form>


 </p>
<p align="center"> </p>
</body>
</html>

Link to comment
Share on other sites

You already had that error a couple of hours ago and were told to use all lowercase products for your table name because it will be case-sensitive if your operating system is case-sensitive.

 

yeah, change

$query = "SELECT * FROM `Products` WHERE `ID` = '".$_REQUEST['product']."'";

to

$query = "SELECT * FROM `products` WHERE `ID` = '".$_REQUEST['product']."'";

 

p.s. - thanks for using code tags :)

Link to comment
Share on other sites

Hello not quite there yet ?

 

Im getting the follwoing error on page when I save the product

 

Parse error: syntax error, unexpected ')', expecting ']' in /home/characte/public_html/test/admin/update-product.php on line 15

 

code below on update product page

 

<? include 'db.php' ?>

<?

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");
$sql_query = mysql_query("UPDATE `products` SET `title` = '".$_REQUEST['title']."',
`size` = '".$_REQUEST['size']."',
`description` = '".$_REQUEST['description']."',
`category_id` = ".$_REQUEST['category_id'].",
`Image` = '".$_REQUEST['image']."',
`Imageenlarge` = '".$_REQUEST['imageenlarge') or die (mysql_error());


// close connection
mysql_close($connection);
?>
<META HTTP-EQUIV=Refresh CONTENT="0; URL=index.php?page=products.php">

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.