Error: Incorrect table name Error – PHP

  php

Q(Question):

THis is i am getting error.here i attached my all codes pls help to avoid this error.

Notice: Undefined index: sname in C:\xampp\htdocs\projects\prabha\admin\subjects\que stion\qinsertcon.php on line 11
INSERT INTO “(id,question,a,b,c,d,answer)values(”,’Which Canadian Prime Minister served from 1921 to 1926, 1921 to 1930, and 1935 to 1948?’,’Pierre Trudeau’,’Sir Robert Borden’,’Louis St. Laurent’,’William Lyon Mackenzie King’,’d’)Error: Incorrect table name ”

Attached Files
File Type: txt inserting problem.txt (5.2 KB, 465 views)

A(Answer):

I don’t open attachments from people I don’t know, but from the error message, it looks like you didn’t specify a table in your code.

A(Answer):

or the variable holding the table name was empty.

A(Answer):

Thank u for your valuable response.i don’t know how to insert correctly on corresponding table.
1.in this blow code is display insert,update,delete options i click insert button inserting page opened.
2.i give Q&a for insert form and finaly submit the button the incorrect table name problem occured here i attached my code blow.

1.qdetails.php

<table align="center" csellpadding="0" bgcolor="#FFFFFF" width="800" border="0">
<th>
<font color="orange" size="+3" >Prepare your question's here.</font>
</th>
<tr>
<td><h1 align="center" class="heading"></h1>
<p align="center">&nbsp;</p>
<table width="638" border="0" align="center">
<tr>
<td width="107"><a href="qinsert.php?id=<?php echo $sname;?>">
<img border="0" name="test
" src="http://bytes.com/images/insert.png" onMouseOver="this.src='images/insert2.png';" onMouseOut="this.src='images/insert.png';" /></a>
</td>
<td width="515" class="options">to enter a new question</td>
</tr>
<tr>
<td>
<a href="qdelete.php?id=<?php echo $sname;?>">
<img border="0" src="http://bytes.com/images/delete.png" onMouseOver="this.src='images/delete2.png';" onMouseOut="this.src='images/delete.png';" /></a>
</td>
<td class="options">to delete a questions</td>
</tr>
<tr>
<td>
<a href="qupdate.php?id=<?php echo $sname;?>"><img border="0" src="http://bytes.com/images/modify.png" onMouseOver="this.src='images/modify2.png';" onMouseOut="this.src='images/modify.png';" /></a>
</td>
<td class="options">to modify a questions</td>
</tr>
</table>

——————————-
2.qinsert.php

<table border="1" align="center">
<tr>
<td>
<table>
<tr>
<td><font color="white" size="+2">Enter your question here:</font>
</td>
</tr>
<tr>
<td><textarea cols="80" rows="6" name="question"></textarea>
</td>
</tr>
<tr>
<td>
<font color="white">A.<input type="text" value="" name="a" size="50"> &nbsp;&nbsp;&nbsp;
Answer:<input type="radio" name="answer" value="a">
</font>
</td>
</tr>
<tr>
<td>
<font color="white">B.<input type="text" value="" name="b" size="50"> &nbsp;&nbsp;&nbsp;
Answer:<input type="radio" name="answer" value="b">
</font>
</td>
</tr>
<tr>
<td>
<font color="white">C.<input type="text" value="" name="c" size="50"> &nbsp;&nbsp;&nbsp;
Answer:<input type="radio" name="answer" value="c">
</font>
</td>
</tr>
<tr>
<td>
<font color="white">D.<input type="text" value="" name="d" size="50">
&nbsp;&nbsp;&nbsp;
Answer:<input type="radio" name="answer" value="d">
</font>
</td>
</tr>
<!--<tr><td><font color="white" size="+2">Answer Description:</font></td></tr>
<tr>
<td>
<textarea cols="80" rows="2" name="description"></textarea>
</td>
</tr>-->
<tr>
<td>
<input type="submit" value="submit">
<input type="reset" value="Reset" name="reset">
</td>
<td>
<a href="qdetails.php">
<img border="0" src="http://bytes.com/images/goback.png" onMouseOver="this.src='images/goback2.png';" onMouseOut="this.src='images/goback.png';" /></a>
</a>
</td>
</tr>
</table>

—————
3. qinsertcon.php db connect code

<?php
$con=mysqli_connect("localhost","root","","test");
if(mysqli_connect_errno())
{
echo "Failed to connect to MYSQL: " .mysqli_connect_error();
}
//if(isset($_POST['sname']))
//{
$sql="INSERT INTO `".$_REQUEST['sname']."`(id,question,a,b,c,d,answer)values('','$_POS T[question]','$_POST[a]','$_POST[b]','$_POST[c]','$_POST[d]','$_POST[answer]')";
//}
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>

A(Answer):

as suspected, the table name variable is empty. but why for pete’s sake do you take the table name from the client? all your other table fields are defined, so an arbitrary table name won’t do. just put in the table name like you did with the field names.

A(Answer):

Thak you Dormilich fianly i got solution here is the correct code.
once again thank u.have a nice day.
print $sql="INSERT INTO ".$_REQUEST[‘sub_name’]."(id,question,a,b,c,d,answer)values(”,’$_POST[question]’,’$_POST[a]’,’$_POST[b]’,’$_POST[c]’,’$_POST[d]’,’$_POST[answer]’)";

A(Answer):

I need to point out that you are highly susceptible to SQL Injection.

LEAVE A COMMENT