Retrieving data from mysql

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>";
/*

Thanks man… It works fine now, But can I make it for 10 questions at the same time? I guess I should do a loop here instead of repeating the work 10 times again…

Can I also do the exact same thing but for the answers as well… In other words I will have two columns one for the questions and one for the answers and I can edit them both and press save to change it all!?

Thanks for your help, I really appreciate it

By the way, if you use the following functions your project code may become neater

<?php
function getQuestion($Number){
    $Number = (int)$Number;
    $Query = MySQL_Query('SELECT Question FROM Questions WHERE QuestionNO = ' . $Number);
    if($Query === false || mysql_num_rows($Query) == 0){
        return false;
    }else{
        return MySQL_Result($Query, 0);
    }
}
function setQuestion($Number, $Question){
    $Number = (int)$Number;
    $Question = MySQL_Real_Escape_String($Question);
    MySQL_Query("UPDATE Questions SET Question = '{$Question}' WHERE QuestionNO = {$Number}");
}
function post($Key){
    if(!array_key_exists($Key, $_POST)){
        return false;
    }else{
        return $_POST[$Key];
    }
}

Put functions like the above into a function file and you can use them throughout your project. An example usage would be the file you already have:

require("connect.php");
if(false !== ($Question = post('tochange1'))){
    setQuestion(1, $Question);
}
echo '
   <form method="post" action="">
       <input type="text" name="tochange1" value="' . getQuestion(1) . '" />
       <input type="submit" name="submit" value="Change" />
    </form>
    <br />
'; 

Of course this is assuming only one question can have a certain question number.

Your code assumes that you’ve already submitted the form and therefore it IS outputting the question (from your above code) but that question is empty because the page is overwriting it.

Try the following:


require("connect.php");
if(array_key_exists('tochange1', $_POST)){
    $tochange1 = $_POST['tochange1'];
    $changeQ1 = mysql_query("UPDATE questions SET Question='$tochange1'  WHERE QuestionNO=1");
}
$resultQ1 = mysql_query("SELECT question FROM questions WHERE  QuestionNO=1");
$Current = mysql_result($resultQ1, 0);
   echo '
   <form method="post" action="">
    <input type="text" name="tochange1" value="' . $Current . '" />
    <input type="submit" name="submit" value="Change" />
</form>
<br />
';

That will show the question in the box before you change it :slight_smile: