Jump to content


Photo

I am having problem in PHP and MYSQL Please help me ......


  • Please log in to reply
3 replies to this topic

#1 shanewaj

shanewaj
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 10 October 2006 - 11:23 PM

I am giving the code where i belive the problem is accouring .... what happend it does not insert in to the data base i have tried all the way to put in to the data base but not working ..
the last line of the code echo the hole things if does not able to inset it show the perfect data ....

<?php

  //when postback
  if ($_POST)
  {
    require_once ('../../mysql_connect.php'); // Connect to the database.

    //calculate the next  product ID
$query_num = mysql_query('select MAX(product_id) from TestP');
$row = mysql_fetch_row($query_num);
 
  if ($row !== false)
  {
  $product_id = $row[0] +1;

  //fetching data
  $product_category = $category;
 
  $product_title = $title;
 
  $product_name = $_POST['product_name'];
  $product_code = $_POST['product_code'];
  $product_quantity = $_POST['product_quantity'];
  $product_weight = (float) $_POST['postage_weight'];
  $product_weight = settype($product_weight, float);
  $product_price = (float) $_POST['product_price'];
  $product_price = settype($product_price, float);
 
  if(isset($_POST['product_dangerous']))
  {
  $product_dangerous = '1';

  }
  else
  {
  $product_dangerous = '0';
  }
  $product_dangerous = settype($product_dangerous, int);
 
    //query to inset
    $query_add_product_range =
    "INSERT INTO TestP SET
product_id = $product_id,
product_category=$product_category,
product_title = $product_title,
product_name = $product_name,
product_code =$product_code,                                                                 
product_quantity_description = $product_quantity
product_weight=$product_weight,
product_price = $product_price,
product_dangerous= $product_dangerous ";

$result_add_range = mysql_query($query_add_product_range);


if($result_add_range)
{
echo'done';
}
else
{
echo  $product_id,' ---', $product_category,' ---', $product_title,' ---', $product_name,' ---', $product_code,' ---', $product_quantity,' ---' ,$product_weight,
      ' ---',$product_dangerous, ' ---';
}
  }
  }

?> 

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 10 October 2006 - 11:30 PM

Make two changes and let's see what happens:

First change: make all of the statements in the INSERT query look like product_id = '$product_id', i.e. add single quotes around the value.

Second change: Change:

$result_add_range = mysql_query($query_add_product_range);

To this:

$result_add_range = mysql_query($query_add_product_range) or die("Error ". mysql_error(). " with query". $query_add_product_range);

Legend has it that reading the manual never killed anyone.
My site

#3 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 10 October 2006 - 11:35 PM

If your doing a INSERT, then you need to change your query, because your using INSERT and UPDATE logic, which will not work....

$query = "INSERT INTO TestP VALUES ( '{$product_id}', '{$product_category}', '{$product_title}', '{$product_name}', '{$product_code}', '{$product_quantity}', '{$product_weight}', '{$product_price}', '{$product_dangerous}' );";

It might not be the right order because I do not know your database scheme, but use this as an example of what a INSERT should look like! Also please take the time to validate your $_POST array, if you don't your asking for trouble...


me!


#4 Firemankurt

Firemankurt
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts
  • LocationWashington State

Posted 10 October 2006 - 11:46 PM

Her try this:

     $query_add_product_range =
"INSERT INTO TestP 
(
product_id,
product_category,
product_title,
product_name,
product_code,
product_quantity_description,
product_weight,
product_price,
product_dangerous
)
VALUES 
(
$product_id,
$product_category,
$product_title,
$product_name,
$product_code,
$product_quantity,
$product_weight,
$product_price,
$product_dangerous 
)";





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users