2 Replies - 2156 Views - Last Post: 30 November 2013 - 05:59 PM Rate Topic: -----

#1 ainyrusly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 07-February 11

an error in add process to database.

Posted 30 November 2013 - 05:44 PM

it said :

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 'category_id='1'' at line 1

I cant find out what is the problems. fyi, category_id is the foreign key for produts table. Is there any possible where I can't add new category_id cause it is a fk for this table? :dozingoff:


<?php
/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/
 
 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($product_name, $product_id, $price, $category_id, $error)
 {
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
 <title>New Record</title>
 </head>
 <body>
 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 
 
 <form action="" method="post">
 <div>
 <p align="center"><strong>Product Name: *</strong> 
   <input type="text" name="product_name" value="<?php echo $product_name; ?>" />
 </p>
 <p align="center"><br/>
   <strong>Product ID: *</strong> 
   <input type="text" name="product_id" value="<?php echo $product_id; ?>" />
 </p>
 <p align="center"><br/>
   <strong>Product price : *</strong> 
   <input type="text" name="price" value="<?php echo $price; ?>" />
 </p>
 <p align="center"><br/>
   <strong>Product Category : *</strong> 
   <input type="text" name="category_id" value="<?php echo $category_id; ?>" /><br/>
   
 </p>
 <p align="center">* required</p>
 <div align="center">
   <input type="submit" name="submit" value="Submit">
 </div>
 </div>
 </form> 
 </body>
 </html>
 <?php 
 }
 
 
 

 // connect to the database
 session_start();
 include 'dbconnect.php';

 
 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $product_name = mysql_real_escape_string(htmlspecialchars($_POST['product_name']));
 $product_id = mysql_real_escape_string(htmlspecialchars($_POST['product_id']));
 $price = mysql_real_escape_string(htmlspecialchars($_POST['price']));
 $category_id = mysql_real_escape_string(htmlspecialchars($_POST['category_id']));
 
 // check to make sure both fields are entered
 if ($product_name== '' || $price == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';
 
 // if either field is blank, display the form again
 renderForm($product_name, $product_id, $price,$category_id, $error);
 }
 else
 {
 // save the data to the database
 mysql_query("INSERT products SET product_name='$product_name', product_id ='$product_id' , price='$price' category_id='$category_id' ")
 or die(mysql_error()); 
 
 // once saved, redirect back to the view page
 header("Location: process_view.php"); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','' ,'');
 }
?> 


Is This A Good Question/Topic? 0
  • +

Replies To: an error in add process to database.

#2 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6554
  • View blog
  • Posts: 26,569
  • Joined: 12-December 12

Re: an error in add process to database.

Posted 30 November 2013 - 05:51 PM

You are missing a comma before category_id.
Was This Post Helpful? 0
  • +
  • -

#3 ainyrusly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 07-February 11

Re: an error in add process to database.

Posted 30 November 2013 - 05:59 PM

wopss, silly mistakes thanks btw
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1