TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   work with multi cheek box to same row (http://www.talkphp.com/absolute-beginners/5879-work-multi-cheek-box-same-row.html)

destroyerx15 05-30-2011 10:45 PM

work with multi cheek box to same row
 
hi guys
i need some help to insert multi cheek box to db in one row.

i make like this
in form.html

<table class="table">
<form action="1.php" method="post">
<tr>


<th class="th">ORDER_DESC </th>

</tr>
<tr>
<td class="td"><input type="checkbox" name="checkbox[]" value="CSF"/> CSF </td>
</tr>

<tr>
<td class="td"><input type="checkbox" name="checkbox[]" value="LFT"/> LFT </td>
</tr>

<tr>
<td class="td"><input type="checkbox" name="checkbox[]" value="RFR"/> RFR </td>
</tr>

<tr>
<td class="td" ><input type="submit" value="Submit" name="Submit"/>
</td>
</tr>
</form>
</table>


in php:

<?php
$orders = $_POST['checkbox'];
mysql_connect("localhost", "root","") or die ('Error: '. mysql_error());
mysql_select_db("project");

if(count($orders)>0)
{
foreach($orders as $key=>$order)
{
$query="INSERT INTO orders (ORDER_DESC) VALUES ('".$order."' )";
mysql_query($query) or die ('Error Updating the Database' . mysql_errno());
}
echo "Order Successfully Placed";
}
else
echo "No Orders";
?>

when i press submit it insert into data but not in one row

can help ?

tony 05-31-2011 01:18 AM

That is because you have:

PHP Code:
$query="INSERT INTO orders (ORDER_DESC) VALUES ('".$order."' )";

which when it is looping it is replacing the query. Instead try assemble the query. like this:

PHP Code:
if(count($orders)>0)
{
  $query = 'INSERT INTO orders (ORDER_DESC) VALUES ';
  //you could loop through it
  /*
  foreach($orders as $order)
  {
    $query .= "('".$order."'), ";
  }
  */

  //or just use implode
  $query .= "('".implode("'), ('", $orders)."')";
 
  //if you used the implode function you don't need this next line
  $query = trim(', ', $query).';';
  mysql_query($query) or die ('Error Updating the Database' . mysql_errno());
  echo "Order Successfully Placed";
}

that would create something like this:

SQL Code:
INSERT INTO orders (ORDER_DESC) VALUES ('CSF'), ('LFT'), ('RFR');

which is how you query a mysql database to insert multiple records.

destroyerx15 05-31-2011 11:26 AM

it dosent work
it give me error in db

core1024 06-01-2011 06:51 AM

How is your DB structured?

destroyerx15 06-01-2011 03:56 PM

i didn't understand what you mean

core1024 06-01-2011 06:32 PM

Do you want to add the values in separate columns or joined in one column?

For all values joined you can just do something like this:
* * *
foreach($orders as $key=>$order) $orders[$key] = mysql_real_escape_string($order);
$orders = implode(', ', $orders);
$sql = "INSERT INTO orders (ORDER_DESC) VALUES ('$orders')";
mysql_query($sql) or die(mysql_error());

destroyerx15 06-01-2011 09:59 PM

for join in one column
tanx core is work =)

destroyerx15 06-01-2011 10:34 PM

i want to retrive the cheek box from db to another form to view

i write like this
my second form

<?php
include("config.php");

$orders = $_POST['checkbox'];
foreach($orders as $key=>$order) $orders[$key] = mysql_real_escape_string($order);
$orders = implode(', ', $orders);
$sql = "INSERT INTO orders (ORDER_DESC) VALUES ('$orders')";
mysql_query($sql) or die(mysql_error());
mysql_close($con)
?>

<table class="table" >
<form action="orders1.php" method="post">

<tr>
<th class="th"> ORDERING_DEPT </th>
</tr>
<tr>
<td class="td"> <input type="text" name="ORDERING_DEPT" /> </td>
</tr>

<tr>
<th class="th"> ORDERING_SERVICE </th>
</tr>
<tr>
<td class="td"> <input type="text" name="ORDERING_SERVICE" /> </td>
</tr>



<tr>
<td class="td"> <input type="text" name="ORDERING_DEPT" /> </td>
</tr>


<td class="td" ><input type="submit" value="Submit" name="Submit"/>
</td>
</tr>

</form>
</table>

and for insert :
<?php
include("config.php");

$result =mysql_query("SELECT ORDER_DESC FROM orders
WHERE ORDER_DESC = 'CSF','LFT'");

$sql="INSERT INTO orders (ORDERING_DEPT,ORDERING_SERVICE)
VALUES
('$_POST[ORDERING_DEPT]','$_POST[ORDERING_SERVICE]')";

echo "<table border='1'>
<tr>
<th>ORDER_DESC</th>
<th>ORDERING_DEPT</th>
<th>ORDERING_SERVICE</th>

</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ORDER_DESC'] . "</td>";
echo "<td>" . $row['ORDERING_DEPT'] . "</td>";
echo "<td>" . $row['ORDERING_SERVICE'] . "</td>";
echo "</tr>";
}
echo "</table>";

it give me error for boolen
and when i want to insert the data only shown the cheekbx

can help

core1024 06-02-2011 07:03 AM

<input type="text" name="ORDERING_DEPT" />
is repeated twice. Not sure if it is on purpose, but you should be careful with this.

SELECT ORDER_DESC FROM orders WHERE ORDER_DESC = 'CSF','LFT'
this should be
SELECT ORDER_DESC FROM orders WHERE ORDER_DESC LIKE '%CSF%' AND ORDER_DESC LIKE '%LFT%'
or if you are completely sure for the content
SELECT ORDER_DESC FROM orders WHERE ORDER_DESC = 'CSF, LFT'
the second variant is faster, but the content have to be exact match.

You should always validate your POST/GET data. If for example someone enters anything quoted there are big chances to blow your inserts. You can just $_POST['ORDERING_DEPT'] = mysql_real_escape_string($_POST['ORDERING_DEPT']); and so on...

destroyerx15 06-02-2011 11:53 AM

i mistake the order dept

but when i wnt to update the db i want to insert another from order_desc to same cheek box to view but not work

destroyerx15 06-02-2011 01:31 PM

i do like this for insert
im trying using update but dosent work and is insert to diffrent order not in the same
i make my primary key auto increment

<?php
include("config.php");
$ORDERING_DEPT = $_POST['ORDERING_DEPT']== mysql_real_escape_string($_POST['ORDERING_DEPT']);
$result = mysql_query("select ORDER_DESC from orders where ORDER_DESC='CSF' '%CSF%' AND ORDER_DESC LIKE '%LFT%' ");

$sql = "INSERT INTO orders (ORDERING_DEPT) VALUES ('$ORDERING_DEPT')";


mysql_query($sql) or die(mysql_error());

mysql_close($con)
?>


for my shown data :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>Untitled Document</title>

</head>
<body>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("project", $con);

$result = mysql_query("SELECT * FROM orders");


echo "<table class='table' >
<tr>
<th class='th'>order_no</th>
<th class='th'>ORDER_TYPE</th>
<th class='th'>ORDER_DESC</th>
<th class='th'>ORDERING_DEPT </th>
<th class='th'>ORDERING_SERVICE</th>
<th class='th'>SENDING_DATE</th>
<th class='th'>DELIVERING_NO</th>
<th class='th'>DELIVERING_DEPT </th>
<th class='th'>DELIVERING_DATE</th>
<th class='th'>PRODECT_CODE </th>
<th class='th'>ORDER_DOSES </th>
<th class='th'>PROFILE_CODE </th>
<th class='th'>LAB_RESULT</th>
<th class='th'>LOW_END</th>
<th class='th'>HIGH_END </th>
<th class='th'>IMAG_CODE </th>
<th class='th'>RADIOLOGY_RESULT </th>
<th class='th'>VERIFY_NO</th>
<th class='th'>STATUS</th>
<th class='th'>URGENCY_CODE </th>
<th class='th'>MEASUREMENT_UNIT </th>
<th class='th'>MEDICINE_ROUTE</th>
<th class='th'>COMMENT </th>
<th class='th'>staff_id</th>
<th class='th'>pat_id </th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class='td' >" . $row['order_no'] . "</td>";
echo "<td class='td' >" . $row['ORDER_TYPE'] . "</td>";
echo "<td class='td' >" . $row['ORDER_DESC'] . "</td>";
echo "<td class='td' >" . $row['ORDERING_DEPT'] . "</td>";
echo "<td class='td' >" . $row['ORDERING_SERVICE'] . "</td>";
echo "<td class='td' >" . $row['SENDING_DATE'] . "</td>";
echo "<td class='td' >" . $row['DELIVERING_NO'] . "</td>";
echo "<td class='td' >" . $row['DELIVERING_DEPT'] . "</td>";
echo "<td class='td' >" . $row['DELIVERING_DATE'] . "</td>";
echo "<td class='td' >" . $row['PRODECT_CODE'] . "</td>";
echo "<td class='td' >" . $row['ORDER_DOSES'] . "</td>";
echo "<td class='td' >" . $row['PROFILE_CODE'] . "</td>";
echo "<td class='td' >" . $row['LAB_RESULT'] . "</td>";
echo "<td class='td' >" . $row['LOW_END'] . "</td>";
echo "<td class='td' >" . $row['HIGH_END'] . "</td>";
echo "<td class='td' >" . $row['IMAG_CODE'] . "</td>";
echo "<td class='td' >" . $row['RADIOLOGY_RESULT'] . "</td>";
echo "<td class='td' >" . $row['VERIFY_NO'] . "</td>";
echo "<td class='td' >" . $row['STATUS'] . "</td>";
echo "<td class='td' >" . $row['URGENCY_CODE'] . "</td>";
echo "<td class='td' >" . $row['MEASUREMENT_UNIT'] . "</td>";
echo "<td class='td' >" . $row['MEDICINE_ROUTE'] . "</td>";
echo "<td class='td' >" . $row['COMMENT'] . "</td>";
echo "<td class='td' >" . $row['staff_id'] . "</td>";
echo "<td class='td' >" . $row['pat_id'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>









</body>
</html>

tony 06-03-2011 01:37 AM

just a note, try putting some formatting/highlight on the code to make it more readable, by writing the code between or at least


All times are GMT. The time now is 08:58 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0