glassfish Posted October 20, 2014 Share Posted October 20, 2014 (edited) The Script: <?php include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/header.php"); ?> <?php include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/connect.php"); ?> <?php if(isset($_GET['thread_id'])) { $tqs = "SELECT * FROM `thread` WHERE `id` = '" . $_GET['thread_id'] . "'"; $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc)); $row = mysqli_fetch_assoc($tqr); ?> <div class="div"> <h1>Admin Modify</h1> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" name="title" value="<?php echo $row['title']; ?>"/><br/> <textarea type="text" name="caption"><?php echo $row['caption']; ?></textarea> <input type="submit" name="admin_update" value="Update" /> </form> </div> <?php // This prints e.g.: // 47 echo "The Thread ID: "; echo $_GET['thread_id']; if(isset($_POST['admin_update'])){ $tqs = "UPDATE `thread` SET `title` = '" . $_POST['title'] . "', `caption` = '" . $_POST['caption'] . "' WHERE `id` = '" . $_GET['thread_id'] . "'"; $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc)); } } ?> <?php include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/footer.php"); ?> I do not see why this is not working. The "UPDATE" SQL statement has worked without the "WHERE" part and modified all of the posts. (With this the database connection works too.) Though, this statement does work in PhpMyAdmin: UPDATE `thread` SET `title` = 'testing', `caption` = 'testing' WHERE `id` = '47'; I added the "WHERE" part to the script and now it is not modifying the text in the table anymore, basically the "updates" do not happen. What could be the issue here? Any suggestions are much appreciated. Also, I have used this way of "syntax style" in PHP, perhaps somebody could be modifying the "UPDATE" SQL statement into a different syntax style so I can try it out, I would much appreciate it. I would also like to ask how the "syntax style" I have used is and if it can be better than this? Edited October 20, 2014 by glassfish Quote Link to comment Share on other sites More sharing options...
Barand Posted October 20, 2014 Share Posted October 20, 2014 First thing to do is echo "$tqs" ; to view the update query that is being submitted Quote Link to comment Share on other sites More sharing options...
glassfish Posted October 20, 2014 Author Share Posted October 20, 2014 (edited) Thanks for the response! The URL is at first: http://localhost/gallerysite/admin_modify.php?thread_id=46 I tried to print the SQL statement on screen like in this following example: if(isset($_POST['admin_update'])){ $tqs = "UPDATE `thread` SET `title` = '" . $_POST['title'] . "', `caption` = '" . $_POST['caption'] . "' WHERE `id` = '" . $_GET['thread_id'] . "'"; $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc)); echo "The query: " . $tqs; } When I click on the "submit button" ("admin_update") then the URL turns into the following: http://localhost/gallerysite/admin_modify.php And I am getting a blank page, the "header", "footer" and the "stylesheet" is still there, though the SQL statement does not get printed on screen. Any suggestions how to have the "printing on screen" of the SQL statement work out with this script? Edited October 20, 2014 by glassfish Quote Link to comment Share on other sites More sharing options...
glassfish Posted October 20, 2014 Author Share Posted October 20, 2014 (edited) Addendum: I tried the following and it worked: The Script: <?php // This prints e.g.: // 47 echo "The Thread ID: "; echo $_GET['thread_id']; //if(isset($_POST['admin_update'])){ $tqs = "UPDATE `thread` SET `title` = 'testing', `caption` = 'testing' WHERE `id` = '46'"; $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc)); echo "The query: " . $tqs; //} } ?> I also tried the following and it worked: <?php // This prints e.g.: // 47 echo "The Thread ID: "; echo $_GET['thread_id']; //if(isset($_POST['admin_update'])){ $tqs = "UPDATE `thread` SET `title` = 'testing', `caption` = 'testing' WHERE `id` = '" . $_GET['thread_id'] . "'"; $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc)); echo "The query: " . $tqs; //} } ?> Printed on Screen: The query: UPDATE `thread` SET `title` = 'testing', `caption` = 'testing' WHERE `id` = '46' Note: The "submit button" in the "if statement" is commented out. It looks like there may have been an issue with the POST method.(?) EDIT: When I bring the "submit button" with the "if statement" back in it does not work anymore, though I am not seeing where the issue is with this. EDIT 2: Okay I am getting the following error message: Undefined index: thread_id Perhaps, this does happen when the URL changes to:(?) http://localhost/gallerysite/admin_modify.php Though, this does happen, after the "submit button" has gotten clicked.(?) So, basically I click on the "submit button" and the "$_GET['thread_id']" variable is becoming not set? Edited October 20, 2014 by glassfish Quote Link to comment Share on other sites More sharing options...
Zane Posted October 20, 2014 Share Posted October 20, 2014 Turn on error_reporting for one. error_reporting(E_ALL); ini_set('display_errors', 1); Are you sure that thread_id is even set? Put in some echos and die(s) above and below figure out when you're script is finishing execution. Quote Link to comment Share on other sites More sharing options...
Solution glassfish Posted October 20, 2014 Author Solution Share Posted October 20, 2014 (edited) Addendum: So, basically this is how I have the script. I have commented the "If statement" with the "$_GET['thread_id']" variable out. <?php include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/header.php"); ?> <?php include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/connect.php"); ?> <?php //if(isset($_GET['thread_id'])) { $tqs = "SELECT * FROM `thread` WHERE `id` = '" . $_GET['thread_id'] . "'"; $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc)); $row = mysqli_fetch_assoc($tqr); ?> <div class="div"> <h1>Admin Modify</h1> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" name="title" value="<?php echo $row['title']; ?>"/><br/> <textarea type="text" name="caption"><?php echo $row['caption']; ?></textarea> <input type="submit" name="admin_update" value="Update" /> </form> </div> <?php //} ?> <?php // This prints e.g.: // 47 echo "The Thread ID: "; echo $_GET['thread_id']; if(isset($_POST['admin_update'])){ $tqs = "UPDATE `thread` SET `title` = 'testing', `caption` = 'testing' WHERE `id` = '" . $_GET['thread_id'] . "'"; $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc)); echo "The query: " . $tqs; } ?> <?php include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/footer.php"); ?> After clicking on the submit button I am getting the following errors: Notice: Undefined index: thread_id in C:\xampp\htdocs\gallerysite\admin_modify.php on line 15 Notice: Undefined index: thread_id in C:\xampp\htdocs\gallerysite\admin_modify.php on line 38 Notice: Undefined index: thread_id in C:\xampp\htdocs\gallerysite\admin_modify.php on line 41 The SQL query printed on screen: UPDATE `thread` SET `title` = 'testing', `caption` = 'testing' WHERE `id` = '' @Zane I am becoming confused, why "thread_id" is even becomes not set anymore.(?) It happens after the "submit button" has gotten clicked. EDIT: I stored "thread_id" in the form as a hidden field, and then I go over to update the table with "$_POST['thread_id']". This works. <div class="div"> <h1>Admin Modify</h1> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="hidden" name="thread_id" value="<?php echo $_GET['thread_id']; ?>"/> <input type="text" name="title" value="<?php echo $row['title']; ?>"/><br/> <textarea type="text" name="caption"><?php echo $row['caption']; ?></textarea> <input type="submit" name="admin_update" value="Update" /> </form> </div> Thanks for the answers! Edited October 20, 2014 by glassfish Quote Link to comment Share on other sites More sharing options...
Zane Posted October 20, 2014 Share Posted October 20, 2014 it was saying thread_id wasn't a defined index because you were echo-ing out the thread_id get variable. You also commented out the if (isset) portion. So it seems the answer is that thread_id isn't being passed when you seem to think it is. If you plan to use the same value on multiple pages, it's best to use the actual tool for that task... $_SESSION. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 20, 2014 Share Posted October 20, 2014 It seems that you are using both POST and GET args here. If you are letting the user enter an id into a form field that has method='POST', then your url is not going to have a thread_id=xxx in it anymore. Quote Link to comment 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.