Jump to content

INSERT AND UPDATE HELP. PHP/MYSQL


Alex1646

Recommended Posts

I am trying to create a script that takes information from a form and puts in a database.

In the action page, I decided to post a URL that shows the user there story that they posted. This is where I ran into the problem. . I realized that for the optional fields I could not just use a seperate insert statement, because this creates a new row. So I desided to use update statments, but this STILL does not work, they values or simply not getting inserted.  Here is the code:

<?php

if(isset($_POST['hidden']))
{

die('SPAM BOT!');
}
if (
!isset($_POST['title']) 
&& !isset($_POST['summary'])
&& !isset($_POST['story'])
&& !isset($_POST['rating'])
&& !isset($_POST['cat'])
)
{
	die("<div id='impor'>You forgot to enter one(or more) of the following fields <br /> 
	1. Title <br />
	2. Summary <br />
	3. Story<br />
	 </div>
	 ");

}


mysqlConnect();


//take data from form an\ put them in variable
$title_form = bb(mysql_real_escape_string($_POST['title'])); //required 
$summ_form = bb(mysql_real_escape_string($_POST['summary']));// required
$story_form = bb(mysql_real_escape_string($_POST['story'])); 
$cat_form = $_POST['cat'];
$rating_form = $_POST['rating'];
$username = $_SESSION['user'];
// Make the other var into a list of links




mysql_query("
INSERT INTO story_info (title, sum, story, user, cat, rating)
VALUES('$title_form','$summ_form', '$story_form,', '$username', '$cat_form','$rating_form')
");
if(isset($_POST['notes']))
{
$notes_form = mysql_real_escape_string($_POST['notes']);
$notes_final = bb($notes_form);
mysql_query("
UPDATE story_info
SET notes = '$notes_final'
WHERE story = '$story_form' AND user = '$username' AND sum = '$summ_form' AND title = '$title_form'
");
}
//put other in array. Use while loop to put link code. Then but it back into one non array variable
if(isset($_POST['u_id']))
{
$uid = mysql_real_escape_string($_POST['u_id']);
$uid_db = str_replace(' ','_', $uid);
$blerg = "
UPDATE story_info
    SET series_id = '$uid_db'
WHERE story = '$story_form' AND user = '$username' AND sum = '$summ_form' AND title = '$title_form'
";
mysql_query($blerg);

}
echo "<h1> Your Story Has Been Posted! Thanks for posting $username .   </h1>";
echo "Please review the post below <br />";
echo "<h2> $title_form </h2>";
echo "<strong> <h2> Summary: </h2> </strong> $summ_form";
echo "<h4> Story: </h4>";
echo "$story_form";
if(isset($notes))
{
echo "<h4> Author's Notes: </h4> ";
echo "$notes_final";


}
if (isset($uid_db))
{
echo '<h3> Unique Series ID </h3>';
echo '<p> Make sure to write down this! <br />' .$uid_db .'</p> ';	
}

$db = mysql_query("
SELECT story_id
FROM story_info
WHERE story='$story_form' AND user='$username'
")or die(mysql_error());
$rows = mysql_fetch_assoc($db);

$id = $rows['story_id'];





echo "Catagory: $cat_form <br />
Rating: $rating_form  <br />
";
echo "<a href='?p=page&id=$id'> Click here to view your story! </a>'";
?>

 

Please help!

Link to comment
Share on other sites

Thats  not it. The values in the update statements are not getting inserted.

Updates don't insert, they only update an existing record.  Are you sure there are values that match your WHERE conditions in the database?  If so, try adding or die(mysql_error()) to the end of your mysql_query() calls.

Link to comment
Share on other sites

Do they work if I inserted some values into a row but not all? I want to insert non optional values, then if the optional are set I want to update the row to insert these values. Will this work?

I put the or die(mysql_error()) at the end of my query statements, but I dont get any errors and it does not die.

Link to comment
Share on other sites

If `story` AND `user` AND `sum` AND `title` do not all match the same record, the query will not update anything, and there will be no error, because that isn't an error condition. The query executes successfully without inserting any data. I'd suggest you make your WHERE clause match the primary key index field of the record, or the user id and another required field instead of trying to match values that may or may not exist.

Link to comment
Share on other sites

If `story` AND `user` AND `sum` AND `title` do not all match the same record, the query will not update anything, and there will be no error, because that isn't an error condition. The query executes successfully without inserting any data. I'd suggest you make your WHERE clause match the primary key index field of the record, or the user id and another required field instead of trying to match values that may or may not exist.

I dont understand why they wouldn't match, but nevertheless you are probably A LOT better then me. SO how can I get the primary key from the insert statement?

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.