Jump to content

[SOLVED] insert into


richard_PHP

Recommended Posts

the worlds most easiest bit of code and it aint working!!!! ive checked the database, the page thats sending the information and een this one to make sure everything is the same but it keeps displaying the error message.

 

code:

 

<?php
			$conn = mysql_connect("xxx", "xxx", "xxx");

			mysql_select_db("xxx", $conn);

			$img = $_POST[img];
			$thumb = $_POST[thumb];
			$url = $_POST[url];
			$title = $_POST[title];
			$description = $_POST[description];
			$type = $_POST[type];

			$sql = "INSERT INTO xxx VALUES ('', '$_POST[img]', '$_POST[thumb]' '$_POST[url]', '$_POST[title]', '$_POST[description]', '$_POST[type]')";
			$result = mysql_query($sql);
			if ($result) {
				echo "<p><strong>$title</strong> was added into the database. Would you like to:</p>";
				echo "<p>- <a href='insert.html'>Add another</a></p>";
			}
			else {
				echo "<p>There was a problem.</p>";
			}
		?>

Link to comment
Share on other sites

had a look into it and found that i needed '' instead of NULL for the id. doing this, it now says that the project was added to the database. when checking..... it isnt.

 

heres the code as it stands at the moment:

 

<?php
			// Start the connection to the database
			$conn = mysql_connect("xxx", "xxx", "xxx");
			// Select the database to use
			mysql_select_db("xxx", $conn);
			// Assign the posted values as variables to use later
			$img = $_POST[img];
			$thumb = $_POST[thumb];
			$url = $_POST[url];
			$title = $_POST[title];
			$description = $_POST[description];
			$type = $_POST[type];
			// Create the MySQL command which adds the user's information into the database.
			$sql = "INSERT INTO xxx (`id`, `img`, `thumb`, `url`, `title`, `description`, `type`) VALUES ('', '$_POST[img]', '$_POST[thumb]' '$_POST[url]', '$_POST[title]', '$_POST[description]', '$_POST[type]')";

			$result = mysql_query($sql);
			if (!$result) {
				echo "<p><strong>$title</strong> was added into the database. Would you like to:</p>";
				echo "<p>- <a href='insert.html'>Add another</a></p>";

			}
			else {
				echo "<p>There was a problem.</p>";
			}
		?>

Link to comment
Share on other sites

Ok, if it isn't inserting into the database, try this

 

A) Delete your query

B) Restart your query, add one column and test

C) Add another column and test

D) Add another column and test

 

If you do that, until you've built your query, you'll either get it right or find the problem

Link to comment
Share on other sites

Use this:

 

$sql = 'INSERT INTO xxx (`id`, `img`, `thumb`, `url`, `title`, `description`, `type`) VALUES ("", ' . $_POST['img'] . ', ' . $_POST['thumb'] . ', ' . $_POST['url'] . ', ' . $_POST['title'] . ', ' . $_POST['description'] . ', ' . $_POST['type'] . ')';

 

Also, just to let you know, you don't have to include the ID in the inserting query if it's an auto-incrementing value.

Link to comment
Share on other sites

Hi

 

The assignment of the variable from the $_POST array probably won't work as you are not putting quotes inside the square brackets, which I presume is why you are trying to use the $_POST array within the SQL.

 

Try this:-

 

<?php
// Start the connection to the database
$conn = mysql_connect("xxx", "xxx", "xxx");
// Select the database to use
mysql_select_db("xxx", $conn);
// Assign the posted values as variables to use later
$img = $_POST['img'];
$thumb = $_POST['thumb'];
$url = $_POST['url'];
$title = $_POST['title'];
$description = $_POST['description'];
$type = $_POST['type'];
// Create the MySQL command which adds the user's information into the database.
$sql = "INSERT INTO xxx (`id`, `img`, `thumb`, `url`, `title`, `description`, `type`) VALUES (NULL, '$img', '$thumb' '$url', '$title', '$description', '$type')";

$result = mysql_query($sql);
if ($result) {
	echo "<p><strong>$title</strong> was added into the database. Would you like to:</p>";
	echo "<p>- <a href='insert.html'>Add another</a></p>";

}
else {
	echo "<p>There was a problem. <br /> $sql <br /> ".mysql_error()."</p>";
}
?>

 

Note that you really should use mysql_real_escape_string for the variable to try and prevent SQL injection issues.

 

All the best

 

Keith

Link to comment
Share on other sites

Just do this.. Bit messy since you gotta list all variables, but meh

(Assuming id is an autoincrement)

 

$img = $_POST['img'];
$thumb = $_POST['thumb'];
$url = $_POST['url'];
$title = $_POST['title'];
$description = $_POST['description'];
$type = $_POST['type'];
// Create the MySQL command which adds the user's information into the database.
$sql = "INSERT INTO xxx (`img`, `thumb`, `url`, `title`, `description`, `type`) VALUES ('" . $img . "', '" . $thumb . "', '" . $url . "', '" . $title . "', '" . $description . "', '" . $type . "')";

 

(also there was a comma missing)

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.