wizowizo Posted June 24, 2010 Share Posted June 24, 2010 Hi there, Here is a basic script that I have created to retrieve some data from my MYSQL database. There is another PHP script for the connection, and everything is okey in connecting with the MYSQL. I am using this script to insert a new data to one of the tables inside my database, but my problem is as follow: I can insert the info that I want, and after pressing the button, the info will be updated and will be showed on the page. I want this info to be shown before I enter the new info inside the text box and press the submit button. In other words, I want to see the retrieved info before and after the submit. but with this code I can only see it after I press the submit button. I am sure, it is a small thing that I am missing here, but am just new to PHP. Any advice would be helpful.. <?php require("connect.php"); $tochange1 = $_POST['tochange1']; $changeQ1 =mysql_query("UPDATE questions SET Question='$tochange1' WHERE QuestionNO=1"); $resultQ1 = mysql_query("SELECT question FROM questions WHERE QuestionNO=1"); while ($row1 = mysql_fetch_assoc($resultQ1)) { echo $row1["question"]; } echo " <form method='POST'> <input type='text' name='tochange1'> <input type='submit' name='submit' value=Change </form> "; echo"<br>"; /* Link to comment https://forums.phpfreaks.com/topic/205810-showing-data-from-mysql/ Share on other sites More sharing options...
sw0o0sh Posted June 25, 2010 Share Posted June 25, 2010 Given the structure of the code, why wouldn't it always be displaying the contents of that particular table? However, since your request seems odd, I'm going to guess you want something like this: <?php require("connect.php"); if(isset($_POST['submit']) && isset($_POST['tochange1'])){ $tochange1 = $_POST['tochange1']; $changeQ1 =mysql_query("UPDATE questions SET Question='" . mysql_real_escape_string($tochange1). "' WHERE QuestionNO = 1"); } $resultQ1 = mysql_query("SELECT question FROM questions WHERE QuestionNO=1"); while($row1 = mysql_fetch_assoc($resultQ1)) { echo $row1["question"]; } echo " <form method='POST'> <input type='text' name='tochange1' /> <input type='submit' name='submit' value='Change' /> </form> <br /> "; ?> Good luck. I've also tweaked it some what, to make sure they're entry isn't blank and added mysql_real_escape_string to the input. Link to comment https://forums.phpfreaks.com/topic/205810-showing-data-from-mysql/#findComment-1077059 Share on other sites More sharing options...
wizowizo Posted June 26, 2010 Author Share Posted June 26, 2010 You are the best!! It works fine now... I'm trying to change it a little bit so I it can show not only one question but 10 questions so I can change them all at the same time. Thanks man Link to comment https://forums.phpfreaks.com/topic/205810-showing-data-from-mysql/#findComment-1077639 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.