Jump to content

Showing Data from MYSQL


wizowizo

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.