hi i want to update or insert to my exit db that have patient id and staff_id

i make lke this for my form :

<table class="table">
<form action="cheekbox.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>

and for 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."' ) SELECT staff_id , pat_id FROM orders ";
mysql_query($query) or die ('Error Updating the Database' . mysql_errno());
}
echo "Order Successfully Placed";
}
else
echo "No Orders";
?>

Recommended Answers

All 6 Replies

Member Avatar for diafol
$query="INSERT INTO orders (ORDER_DESC) VALUES ('".$order."' ) SELECT staff_id , pat_id FROM orders

What are you trying to do? staff and patient - from where does this info come?

'Error Updating the Database' - you're not updating, you're inserting.

i mean insert into the same record
but when i want if i delete

SELECT staff_id , pat_id FROM orders

it will insert but not in the same record so i try to update by insert

is there any way to insert by the same record ?

Member Avatar for diafol

I really don't understand you.

INSERT ON DUPLICATE KEY UPDATE

or

REPLACE INTO

The first is probably the best method as the second has certain issues with some types of tables.

//get staff_id, $pat_id from somewhere
//$order from form
//The table PK should be staff_id AND pat_id

INSERT INTO `orders` VALUES ($staff_id,$pat_id,'$order') ON DUPLICATE KEY UPDATE `order` = '$orders';

HOWEVER, I see that you've got multiple orders for the same staff_id/pat_id combination. This totally wrecks the above.

It could still work (*presumably*) is PK was all three fields together.

Otherwise,manually check for the existence of a record corresponding to your data, if it exists, do nothing, else, insert.

I don't see why you need to DELETE anything.

how to get staff_id, $pat_id ??
the two is exist in my db as foregin key to another table

my primary key is order_no and auto increment
so
i make like this

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("project", $con);

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

echo "<table class='table' >
<tr>
<th class='th'>patient id </th>
<th class='th'>date of birth</th>
<th class='th'>family name</th>
<th class='th'>dather name </th>
<th class='th'>given name</th>
<th class='th'>sex </th>
<th class='th'>status </th>


</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td class='td' ><a href='lab.php?pat_id=".$row['pat_id']."'>".$row['pat_id']."</a></td> ";
  echo "<td class='td' >" . $row['DOB'] . "</td>";
  echo "<td class='td' >" . $row['FAMILY_NAME'] . "</td>";
  echo "<td class='td' >" . $row['FATHER_NAME'] . "</td>";
  echo "<td class='td' >" . $row['GIVEN_NAME'] . "</td>";
  echo "<td class='td' >" . $row['SEX'] . "</td>";
  echo "<td class='td' >" . $row['STATUS'] . "</td>";

  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

from here i m making link by patient id and form here i mtrying to get paitent id
can help ?

Member Avatar for diafol
if(isset($_GET['pat_id'])){
   $pat_id = mysql_real_escape_string($_GET['pat_id']);
   ...
}

that it?

i try to put like you said

but also give me error in update

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.