Alex1646 Posted March 10, 2011 Share Posted March 10, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/230262-insert-and-update-help-phpmysql/ Share on other sites More sharing options...
Alex1646 Posted March 10, 2011 Author Share Posted March 10, 2011 Ive checked my db and the values in the optional fields are not getting inserted, Quote Link to comment https://forums.phpfreaks.com/topic/230262-insert-and-update-help-phpmysql/#findComment-1185773 Share on other sites More sharing options...
Maq Posted March 10, 2011 Share Posted March 10, 2011 I don't think this line is valid (extra comma): VALUES('$title_form','$summ_form', '$story_form,', '$username', '$cat_form','$rating_form') Quote Link to comment https://forums.phpfreaks.com/topic/230262-insert-and-update-help-phpmysql/#findComment-1185779 Share on other sites More sharing options...
Alex1646 Posted March 10, 2011 Author Share Posted March 10, 2011 Thats not it. The values in the update statements are not getting inserted. Quote Link to comment https://forums.phpfreaks.com/topic/230262-insert-and-update-help-phpmysql/#findComment-1185784 Share on other sites More sharing options...
Maq Posted March 10, 2011 Share Posted March 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230262-insert-and-update-help-phpmysql/#findComment-1185788 Share on other sites More sharing options...
Alex1646 Posted March 10, 2011 Author Share Posted March 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230262-insert-and-update-help-phpmysql/#findComment-1185794 Share on other sites More sharing options...
Pikachu2000 Posted March 10, 2011 Share Posted March 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230262-insert-and-update-help-phpmysql/#findComment-1185802 Share on other sites More sharing options...
Alex1646 Posted March 10, 2011 Author Share Posted March 10, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/230262-insert-and-update-help-phpmysql/#findComment-1185843 Share on other sites More sharing options...
jcbones Posted March 11, 2011 Share Posted March 11, 2011 Do you have a auto_increment primary key? To get the last one from a generated, use: mysql_insert_id() Quote Link to comment https://forums.phpfreaks.com/topic/230262-insert-and-update-help-phpmysql/#findComment-1185978 Share on other sites More sharing options...
Alex1646 Posted March 11, 2011 Author Share Posted March 11, 2011 Thank you! Works perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/230262-insert-and-update-help-phpmysql/#findComment-1186020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.