4 Replies - 1175 Views - Last Post: 05 February 2018 - 01:10 AM Rate Topic: -----

#1 phpnewb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-January 16

How To Insert Multiple Combobox Values To Table

Posted 07 January 2016 - 05:57 AM

I have combobo.also user can add multiple comboboxes when button (+)clicked..what i want to do is,when user add multiple combobox and submit.it should be insert to mysql table row by row.this is my code.

<?php	
if(isset($_POST['btn-testing']))
{	
$qat = serialize($_POST['qat']);
mysql_query("INSERT INTO aulogtrail(euser) VALUES('$qat')");
?>
<script>
alert("Report successfully Updated");
window.location="?QADB=home";
</script>
<?php
}
?>	
<script language="javascript">
var i = 1;
function changeIt()
{

my_div.innerHTML = my_div.innerHTML +"<br><select name='qat[]'+ i><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select>"
i++;
}
</script>

<form name="form" action="post">
<select name='qat[]'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="button" value="+" onclick="changeIt()">
<div id="my_div"></div>

<button type="submit" name="btn-testing" class="button" >Update</button>
</form>



thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: How To Insert Multiple Combobox Values To Table

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4154
  • View blog
  • Posts: 13,146
  • Joined: 08-June 10

Re: How To Insert Multiple Combobox Values To Table

Posted 07 January 2016 - 06:36 AM

1) mysql_* functions are deprecated and removed from the current version of PHP

2) you’re highly susceptible to SQL Injection

3) line #4, why do you serialise the input here? in neither case this gives you a sensible data item to be used in a database

4) Prepared Statements make that a piece of cake
// prepare a statement
$stmt = $pdo->prepare('INSERT INTO aulogtrail (euser) VALUES (?)');
// bind the parameter that will later hold the value
$stmt->bindParam(1, $euser, PDO::PARAM_INT);
// execute for each value
foreach ($_POST['qat'] as $euser) {
    $stmt->execute();
}


Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3732
  • View blog
  • Posts: 13,569
  • Joined: 08-August 08

Re: How To Insert Multiple Combobox Values To Table

Posted 07 January 2016 - 07:26 AM

Three languages (4 if you count sql) in one file! That won't be confusing. :whistling:
You need to pick and consistently use an indent style.
Was This Post Helpful? 0
  • +
  • -

#4 tedchong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 31-December 17

Re: How To Insert Multiple Combobox Values To Table

Posted 03 February 2018 - 08:33 PM

// prepare a statement

$stmt = $pdo->prepare('INSERT INTO aulogtrail (euser) VALUES (:Auser)');

// bind the parameter that will later hold the value

$stmt->bindParam(:Auser, $euser, PDO::PARAM_INT);

// execute for each value

foreach ($_POST['qat'] as $euser) {

    $stmt->execute();

}





i change the placeholder (?) to (:Auser) at line 3 and change the value 1 to :Auser at line 7. Is that correct? If anything wrong, pls let me know. Thanks
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4154
  • View blog
  • Posts: 13,146
  • Joined: 08-June 10

Re: How To Insert Multiple Combobox Values To Table

Posted 05 February 2018 - 01:10 AM

Quote

Is that correct?

No, that'll cause a parser error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1