Question:
PHP + MySQL Error - SQL Syntax?
anonymous
2010-12-15 05:45:12 UTC
I am trying to run a script which will display the contents of a database table. You select which table via a radio button. I keep getting this error.

"Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '''' at line 1"

If I do not check for the error the code will run and display the table but If I do not select a table it will produce the following statements.

Warning: mysql_num_fields(): supplied argument is not a valid MySQL result resource on line 33

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 39

My Code:

$selectedRadio = $_POST[options];


$sql = "SELECT * FROM '$selectedRadio'";
//$q = $db->query($sql) or die("SELECT Error: ".mysql_error());
$result = mysql_query($sql); //or die ('Error: '.mysql_error ());
//$r = $q->numRows();


echo "\n";
$i = 0;
while ($i < mysql_num_fields($result)) {
echo "";
$i++;
}
echo "";

while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
echo "";
foreach ($row as $data)
echo "";
echo "";
}
echo "
". mysql_field_name($result, $i) . "
$data
\n";

echo "Table not displayed";


Does anyone know what I'm doing wrong?
Four answers:
steve
2010-12-15 05:56:36 UTC
$sql = "SELECT * FROM '$selectedRadio'"; break out of your quotes



and i assume you connect to your host right, and select the database. you're just not showing it for privacy purposes



and im not positive, its been a while since i've worked with php... but this

$selectedRadio = $_POST[options];

is supposed to be

$selectedRadio = $_POST['options'];
rexroat
2016-10-19 14:04:30 UTC
Why the message identification is null and that's regular key. regular key should not be null, that's used for indexing try working the question for my section first, in mysql command instant
omick16
2010-12-15 05:58:02 UTC
my guess is that the problem is on this line: $selectedRadio = $_POST[options];



you should validate first if the form has been submitted otherwise the $selectedRadio is not set...



you can do this:



if (isset($_POST[options])) {

$selectedRadio = $_POST['options];

...rest of your code here

}
anonymous
2010-12-17 02:31:51 UTC
asdf


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...