Aadishesh Sharma Aadishesh Sharma - 1 year ago 63
SQL Question

How to insert data into table taking table name value from form?

<? if($_POST['submit'])
{ $taskSelect =$_POST['taskOption'];

$sql =array();
foreach($_POST['task'] as $key => $value){
$sql[] =mysql_real_escape_string($value);
$ab ="INSERT INTO" .$taskSelect. "(u_id,task) VALUES ('".$_SESSION['id']."','".implode(',', $sql)."')";
$asc =mysql_query($ab);


Is it possible to do so , As i've tried this but no value is being inserted into table when I'm using table name taken from form?

<select name="taskOption">
<option value="today">Today</option>
<option value="weekly">Weekly</option>
<option value="month">Monthly</option>

Answer Source

You miss some spaces:

$ab ="INSERT INTO" .$taskSelect. "(u_id,task) VALUES ('".$...

should be

$ab ="INSERT INTO " .$taskSelect. " (u_id,task) VALUES ('".$...

That being said, do not use mysql_* anymore, read up on prepared statements and input validation. You are wide open to SQL injection right now.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download