Jump to content

[SOLVED] INSERT QUERY... not inserting.


hellonoko

Recommended Posts

Here is my code again. Not sure what im missing. But it is not inserting. No errors either...

 

Thanks.

 

<?php

include 'dbconnect.php';

$title = $HTTP_GET_VARS[title];
$description = $HTTP_GET_VARS[description];
$media = $HTTP_GET_VARS[media];
$price = $HTTP_GET_VARS[price];
$imagelink = $HTTP_GET_VARS[image_link];

$action = $HTTP_GET_VARS[action];

echo $title;
echo "<br>";
echo $description;
echo "<br>";
echo $media;
echo "<br>";
echo $price;
echo "<br>";
echo $image_link;
echo "<br>";

if ($action == "Add")
{
	$query = "INSERT INTO gallery (title, description, media, price, image) VALUES ($title, $description, $media, $price, '.jpg')" or die (mysql_error('Error, insert query failed'));
	$result = mysql_query($query);
}


?>

<form name="add_gallery_item" method="get" action="add_gallery_item.php">
  <label>
  title:
<input type="text" name="title" id="title">
  </label>
  <p>description:  
    <label>
    <input type="text" name="description" id="description">
    </label>
  </p>
  <p>media:
    <label>
    <input type="text" name="media" id="media">
    </label>
</p>
  <p>price:
    <label>
    <input type="text" name="price" id="price">
    </label>
  </p>
  <p>imagelink:
    <label>
    <select name="image_link" id="image_link">
    </select>
    </label>
  </p>
  <p>
    <label>
    <input type="submit" name="action" id="add_item" value="Add">
    </label>
  </p>
</form>

Link to comment
https://forums.phpfreaks.com/topic/48197-solved-insert-query-not-inserting/
Share on other sites

That's not the problem. The problem is that you are putting the OR DIE part when you are defining the query variable. The OR DIE needs to follow the running of the query:

 

$query = "INSERT INTO gallery (title, description, media, price, image)

              VALUES ($title, $description, $media, $price, '.jpg')";

$result = mysql_query($query)  or die (mysql_error());

Remove the "$result =" - so it's just mysql_query($query)

 

Nothing wrong with that at all.

 

Change:

$query = "INSERT INTO gallery (title, description, media, price, image) VALUES ($title, $description, $media, $price, '.jpg')" or die (mysql_error('Error, insert query failed'));
$result = mysql_query($query);

 

To:

$query = "INSERT INTO gallery (title, description, media, price, image) VALUES ($title, $description, $media, $price, '.jpg')";
$result = mysql_query($query)  or die ("Error: ". mysql_error(). " with query ". $query);

 

The .jpg looks a little suspect. Not sure what you're trying for.

You dont really have much in the way of error handling and your use of mysql_error is invalid. Lots of errors in this code really, try....

 

<?php

  include 'dbconnect.php';

  // these will also generate warnings if they don't exist, better to use...
  // $title = $_GET['title'] ? mysql_real_escape_string($_GET['title']) : '';
  $title = mysql_real_escape_string($_GET['title']);
  $description = mysql_real_escape_string($_GET['description']);
  $media = mysql_real_escape_string($_GET['media']);
  $price = mysql_real_escape_string($_GET['price']);
  $imagelink = mysql_real_escape_string($_GET['image_link']);
  $action = mysql_real_escape_string($_GET['action']);

  echo $title;
  echo "<br>";
  echo $description;
  echo "<br>";
  echo $media;
  echo "<br>";
  echo $price;
  echo "<br>";
  echo $image_link;
  echo "<br>";

  if ($action == "Add") {
    $query = "INSERT INTO gallery (title, description, media, price, image) VALUES ('$title', '$description', '$media', '$price', '.jpg')";
    if ($result = mysql_query($query)) {
      echo "success.";
    } else {
      echo "Failed" . mysql_error();
    }
  }

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.