Jump to content

Recommended Posts

Hello I am a newbie working with PHP and SQL using MySQLi API and am really confused at how to call a certain function.  I need to add a product to the data base with a price and description.  The error is in the addProduct-handler.php @ Product::create($product);  I am not even sure if i am callin it right, but it keeps coming up with this error:

 

Fatal error: Uncaught exception 'Exception' with message '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 ')' at line 2' in /PhpStore/model/Product.php:26 Stack trace: #0 /PhpStore/admin/addProduct-handler.php(25): Product::create(NULL) #1 {main} thrown in /PhpStore/model/Product.php on line 26

 

now I am pretty sure the SQL is correct because i tested it and it allows me to insert, select, and drop tables.

 

Does anyone have any ideas, thanks in advance for the help?

 

 
<?php //new-product.php
$file = basename($_SERVER['PHP_SELF']);
$title = "Add a new product ($file)";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
<html>
	<head>
		<title><?= $title ?></title>
	</head>
<body>
	<h2><?= $title ?></h2>

	<form action="addProduct-handler.php">
		<table class="entry">
			<tr>
				<td class="left">Price</td>
				<td><input type="text" name="price"/></td>
			</tr>
			<tr valign="top">
				<td class="left">Description</td>
				<td><textarea name="dsc" rows="8"></textarea></td>	
			</tr>
	</table>
	<input type="submit" name="add_product" value="Add Product"/>
	</form>
</body>
</html>

 

<?php	//addProduct-handler.php
$top = dirname(dirname(__FILE__));
require "$top/model/Product.php";


    $param = array_merge( $_GET, $_POST );

$price = $param["price"];
$dsc = $param["dsc"];

$esc_price = htmlspecialchars($price);
$esc_dsc = htmlspecialchars($dsc);

$title = "Added new Product";

$file = basename($_SERVER['PHP_SELF']);
$title .= "($file)";

echo "<h2>$title</h2>";

echo "price: $esc_price<br />";						//just for testing
echo "description: $esc_dsc<br />";

echo "***Add Product<br />";
Product::create($product);

?>

 

<?php	//Product.php
require_once "db/Database.php";

class Product {
  private static $table = "products";
  private static $table_def = "id INT AUTO_INCREMENT NOT NULL,
  		       description TEXT,
  		       price DECIMAL(9,2),
  		       PRIMARY KEY(id)";
  
  public function createTable() {
    $cx = Database::connect();
    $table = self::$table;
    $table_def = self::$table_def;
    $cx->query( "DROP TABLE IF EXISTS $table" ) or die($cx->error);
    $cx->query( "CREATE TABLE $table ($table_def)" ) or die($cx->error);
  }

  public function create( $product ) {
    $cx = Database::connect();
    $table = self::$table;
    $description = addslashes($product['description']);
    $price = $product['price'];
    if ( !$cx->query( "INSERT INTO $table (description,price) 
                       VALUES ('$description', $price)") 
       ) throw new Exception($cx->error);
  }

  public function find( $id ) {
    $cx = Database::connect();
    $table = self::$table;
    $res = $cx->query("SELECT * FROM $table WHERE id=$id") or die($cx->error);
    return $res->fetch_assoc();
  }

  public function findAll( $fields = null ) { // if null, assume all fields
    $cx = Database::connect();
    $table = self::$table;
    $field_list = ($fields != null) ? join(",", $fields) : "*";
    $res = $cx->query( "SELECT $field_list FROM $table" ) or die($cx->error);
    $all = array();
    while ($map = $res->fetch_assoc()) $all[] = $map;
    return $all;
  }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/98216-php-5-mysql-funtion-call-help/
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.