Jump to content

PHP & MySQL 'INSERT INTO' function not working.


nickholt1972

Recommended Posts

Here's a snippet of the code i'm working on (for info, the page which feeds the data to this one is a basic form which gathers all the required info):

[code]$productid = $_REQUEST['productid'];
$alphaname = $_REQUEST['alphaname'];
$alphatype = $_REQUEST['alphatype'];
$subcat = $_REQUEST['subcat'];
$manufacturer = $_REQUEST['manufacturer'];
$size = $_REQUEST['size'];
$colour = $_REQUEST['colour'];
$pattern = $_REQUEST['pattern'];
$fastening = $_REQUEST['fastening'];
$description1 = $_REQUEST['description1'];
$description2 = $_REQUEST['description2'];
$description3 = $_REQUEST['description3'];
$smlimage = $_REQUEST['smlimage'];
$lgeimage = $_REQUEST['lgeimage'];
$addbasket = $_REQUEST['addbasket'];
$price = $_REQUEST['price'];
$display = $_REQUEST['display'];

echo '<b>Product ID: </b>' . $productid . '<br />';
echo '<b>AlphaName: </b>' . $alphaname. '<br />';
echo '<b>AlphaType: </b>' . $alphatype. '<br />';
echo '<b>Subcat: </b>' . $subcat. '<br />';
echo '<b>Manuf: </b>' . $manufacturer. '<br />';
echo '<b>Size: </b>' . $size. '<br />';
echo '<b>Colour: </b>' . $colour. '<br />';
echo '<b>Pattern: </b>' . $pattern. '<br />';
echo '<b>Fast: </b>' . $fastening. '<br />';
echo '<b>Desc: </b>' . $description1. '<br />';
echo '<b>Desc: </b>' . $description2. '<br />';
echo '<b>Desc: </b>' . $description3. '<br />';
echo '<b>Sml Image: </b>' . $smlimage. '<br />';
echo '<b>Lge Image: </b>' . $lgeimage. '<br />';
echo '<b>Add Basket Text: </b>' . $addbasket. '<br />';
echo '<b>Price: </b>' . $price. '<br />';
echo '<b>Display: </b>' . $display. '<br /><br /><br />';


$insertdata = "INSERT INTO stocks SET
productid='$productid',
alphatype='$alphatype',
alphaname='$alphaname',
subcat='$subcat',
manufacturer='$manufacturer',
size='$size',
colour='$colour',
pattern='$pattern',
fastening='$fastening',
description1='$description1',
description2='$description2',
description3='$description3',
smlimage='$smlimage',
lgeimage='$lgeimage',
addbasket='$addbasket',
price='$price',
display='$display'";

if (!$insertdata) {
  exit('<p>Unable to insert data:</p>');
}[/code]

So what's happening is, the top section of code gathers the data from the input form and I know this is working because the second section of code displays it on the page perfectly. The problem is with the final section of code which should just pop the data into my database. All that happens is the data displays on the page (so there's no syntax errors) but when I go to my display data page, there's nothing there.
All my connect settings are fine, it just something wrong with the INSERT INTO .... command.
Can anyone help, i'm really at a loss now.
Thanks,
Nick Holt.
Link to comment
Share on other sites

You are not using mysql_query to perform the query. You should do this:
[code]mysql_query($insertdat) or die("<p>Unable to insert data:</p>');[/code]
Instead of:
[code]if (!$insertdata) {
  exit('<p>Unable to insert data:</p>');
}[/code]
Also make sure you are connect to MySQL too.
Link to comment
Share on other sites

And to save you a lot of wasting time while typing I suggest you use:

[code]
// This comes on top of your script or wherever you want your $productid, and everything to start
extract($_REQUEST);

// Now will this still work
echo '<b>Product ID: </b>' . $productid . '<br />';
echo '<b>AlphaName: </b>' . $alphaname. '<br />';
echo '<b>AlphaType: </b>' . $alphatype. '<br />';
echo '<b>Subcat: </b>' . $subcat. '<br />';
echo '<b>Manuf: </b>' . $manufacturer. '<br />';
echo '<b>Size: </b>' . $size. '<br />';
echo '<b>Colour: </b>' . $colour. '<br />';
echo '<b>Pattern: </b>' . $pattern. '<br />';
echo '<b>Fast: </b>' . $fastening. '<br />';
echo '<b>Desc: </b>' . $description1. '<br />';
echo '<b>Desc: </b>' . $description2. '<br />';
echo '<b>Desc: </b>' . $description3. '<br />';
echo '<b>Sml Image: </b>' . $smlimage. '<br />';
echo '<b>Lge Image: </b>' . $lgeimage. '<br />';
echo '<b>Add Basket Text: </b>' . $addbasket. '<br />';
echo '<b>Price: </b>' . $price. '<br />';
echo '<b>Display: </b>' . $display. '<br /><br /><br />';

// What others did not notice is that your query is also wrong.
$insertdata = "`productid`, `alphatype`, etc...";
$query = "INSERT INTO stocks ( %s ) VALUES ( %s )";
// if you are inserting respectively with the columns, you can also use $query = "INSERT INTO stocks VALUES ( %s )";
// ofcourse this way you don't need $insertdata

// Also this is alot of sql code to parse, so check first what is set and what is not!
$value = '';
foreach ($_REQUEST as $k => $v) {
  if ($v) {
      $value .= "'" . $v . "',";
  }
}

// Then to make sure that we don't have a , as last character in our values variable
if (substr($value, -1) == ',') {
  $value = substr($value, 0, strlen($value)-1);
}

$Sql = sprintf($query, $insertdata, $value);

// For your DB and everything, you should use something lik
if (!($Resource = mysql_connect($Host, $User, $Pass))) {
   die('could not connecto to datbase');
}
if (!mysql_select_db($Database)) {
   die('selecting database failed.');
}
// Then parsing your query
if (!mysql_query($Sql, $Resource)) {
   echo "query failed";
} else {
   echo 'jipii';
}
[/code]
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.