Better way of doing this

Its me again. xD

I have this:

[php]

<?php include ("dbc.php"); $key = $_POST['item']; $jobid =filter($_POST['jid']); $add1 = filter($_POST["add1"]); $add2 = filter($_POST["add2"]); $add3 = filter($_POST["add4"]); $pc = filter($_POST["pc"]); $from = filter($_POST["from"]); $to = filter($_POST["to"]); $equipment = filter($_POST["equipment"]); $cname = filter($_POST["cname"]); $del = filter($_POST["deliver"]); foreach($key as $value){ $i = ltrim($value['code'], "0"); $q = $value['quantity']; $d = $value['desc']; $p = $value['price']; $tp = $value['totalprice']; echo "I: ".$i.""; echo "Q: ".$q.""; echo "D: ".$d.""; echo "P: ".$p.""; echo "TP: ".$tp.""; echo "jobid: ".$jobid.""; $sql = "INSERT INTO job_items (job_id, quantity, product_id, unit_price, description, total) VALUES ('$jobid', '$q', '$i', '$p', '$d', '$tp');"; $result = mysql_query($sql); } $sql = "INSERT INTO jobs (job_id, address1, address2, address3, postcd, cid, deliver, fromdate, todate) VALUES ('$jobid', '$add1', '$add2', '$add3', '$pc', '$cname', '$del', '$from', '$to');"; mysql_query($sql); if(!$result ){ die('Could not update data: ' . mysql_error()); } echo "Job inserted successfully\n"; ?>

[/php]

Is there a better way to do it? I feel that its a bit of a hack. It works though.

Where is the information for $_POST[‘item’] coming from.

Hi this script: http://www.phphelp.com/forum/index.php?topic=18230.new;topicseen#new

Other than putting the query all in one string instead of 2, i don’t see a better way of doing it. Why mess with it if it does what its supposed to.

Fair enough. :slight_smile: Thanks. I just seemed a rather long way to do something when normally there is a quicker way to do it.

Well, you can technically do the filter stuff in the query instead of assigning a new variable. Since you’re not doing any validation, it would work.

$result = mysql_query(“INSERT INTO job_items (job_id, quantity, product_id, unit_price, description, total) VALUES (’”.filter($_POST[‘jid’]."’, ‘$q’, ‘$i’, ‘$p’, ‘$d’, ‘$tp’)"); and so on.

I like to use variables in the SQL code. Makes it easier to update if needed. :slight_smile: but i see what you mean.

Sponsor our Newsletter | Privacy Policy | Terms of Service