0

Hello.
I am having a bit problem with the insert query, Every thing is fine but don't know why its not inserting the queries.
Have a look onm code;

function insert()
{
var table=document.getElementById("myTable1");
var table=document.getElementById("myTable");
<?php
$con=mysqli_connect("localhost","root","","bsc_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
//Inserting the basic information of the PATIENT;
$sql="INSERT INTO pat_t (pid, pname, fhname, gender, age, adate, ddate, tby, issue, gtotal, visit)
    VALUES
    ('$_POST[pid]','$_POST[pname]','$_POST[fhname]','$_POST[gender]','$_POST[age]','$_POST[adate]','$_POST[ddate]','$_POST[tby]','$_POST[issue]','$_POST[gtotal]','$_POST[visit]')";

    if (!mysqli_query($con,$sql))
    {
    //die('Error: ' . mysqli_error());
    }

//qty 1 
if ('$_POST[qty]' != "")
{
    $sql1="INSERT INTO pmed_t (visit, pid, medname, qty, rate, total)
    VALUES
    ('$_POST[visit]','$_POST[pid]','$_POST[med]','$_POST[qty]','$_POST[rate]','$_POST[total]')";

    if (!mysqli_query($con,$sql1))
    {
    //die('Error: ' . mysqli_error());
    }
}   
echo "";

//qty 2 
if ('$_POST[qty2]' != "")
{
    $sql2="INSERT INTO pmed_t (visit, pid, medname, qty, rate, total)
    VALUES
    ('$_POST[visit]','$_POST[pid]','$_POST[med2]','$_POST[qty2]','$_POST[rate2]','$_POST[total2]')";

    if (!mysqli_query($con,$sql2))
    {
    //die('Error: ' . mysqli_error());
    }
}   
echo "";

//qty 3 
if ('$_POST[qty3]' != "")
{
    $sql3="INSERT INTO pmed_t (visit, pid, medname, qty, rate, total)
    VALUES
    ('$_POST[visit]','$_POST[pid]','$_POST[med3]','$_POST[qty3]','$_POST[rate3]','$_POST[total3]')";

    if (!mysqli_query($con,$sql3))
    {
    //die('Error: ' . mysqli_error());
    }
}   
echo "";

//qty 4 
if ('$_POST[qty4]' != "")
{
    $sql4="INSERT INTO pmed_t (visit, pid, medname, qty, rate, total)
    VALUES
    ('$_POST[visit]','$_POST[pid]','$_POST[med4]','$_POST[qty4]','$_POST[rate4]','$_POST[total4]')";

    if (!mysqli_query($con,$sql4))
    {
    //die('Error: ' . mysqli_error());
    }
}   
echo "";

//qty 5 
if ('$_POST[qty5]' != "")
{
    $sql5="INSERT INTO pmed_t (visit, pid, medname, qty, rate, total)
    VALUES
    ('$_POST[visit]','$_POST[pid]','$_POST[med5]','$_POST[qty5]','$_POST[rate5]','$_POST[total5]')";

    if (!mysqli_query($con,$sql5))
    {
    //die('Error: ' . mysqli_error());
    }
}   
echo "";

//qty 6 
if ('$_POST[qty6]' != "")
{
    $sql6="INSERT INTO pmed_t (visit, pid, medname, qty, rate, total)
    VALUES
    ('$_POST[visit]','$_POST[pid]','$_POST[med6]','$_POST[qty6]','$_POST[rate6]','$_POST[total6]')";

    if (!mysqli_query($con,$sql6))
    {
    //die('Error: ' . mysqli_error());
    }
}   
echo "";

//qty 7 
if ('$_POST[qty7]' != "")
{
    $sql7="INSERT INTO pmed_t (visit, pid, medname, qty, rate, total)
    VALUES
    ('$_POST[visit]','$_POST[pid]','$_POST[med7]','$_POST[qty7]','$_POST[rate7]','$_POST[total7]')";

    if (!mysqli_query($con,$sql7))
    {
    //die('Error: ' . mysqli_error());
    }
}   
echo "";

//qty 8 
if ('$_POST[qty8]' != "")
{
    $sql8="INSERT INTO pmed_t (visit, pid, medname, qty, rate, total)
    VALUES
    ('$_POST[visit]','$_POST[pid]','$_POST[med8]','$_POST[qty8]','$_POST[rate8]','$_POST[total8]')";

    if (!mysqli_query($con,$sql8))
    {
    //die('Error: ' . mysqli_error());
    }
}   
echo "";

//qty 9 
if ('$_POST[qty9]' != "")
{
    $sql9="INSERT INTO pmed_t (visit, pid, medname, qty, rate, total)
    VALUES
    ('$_POST[visit]','$_POST[pid]','$_POST[med9]','$_POST[qty9]','$_POST[rate9]','$_POST[total9]')";

    if (!mysqli_query($con,$sql9))
    {
    //die('Error: ' . mysqli_error());
    }
}   
echo "";

//qty 10 
if ('$_POST[qty10]' != "")
{
    $sql10="INSERT INTO pmed_t (visit, pid, medname, qty, rate, total)
    VALUES
    ('$_POST[visit]','$_POST[pid]','$_POST[med10]','$_POST[qty10]','$_POST[rate10]','$_POST[total10]')";

    if (!mysqli_query($con,$sql10))
    {
    //die('Error: ' . mysqli_error());
    }
}   
echo "";

mysqli_close($con);

?>
}

When I check the database on localhost. It is inserting only one row in the pat_t table and pmed_t table. every thing(field in the DB table) is showing "0".
Please help me to find out where i am making a mistake...

Thanks n Regards.

Edited by pritaeas: Moved.

4
Contributors
7
Replies
29
Views
4 Years
Discussion Span
Last Post by EvolutionFallen
0

Why is there javascript code? Your if's will always fail. Use:

if ($_POST['qty'] != "")

Edited by pritaeas

0

Sorry forget to mention that I am trigering this insert function on a button's "OnClick" action.
And i have tried your suggestion, Its still having the same issue.

0

Sorry forget to mention that I am trigering this insert function on a button's "OnClick" action.

You are mixing PHP and Javascript, that's not possible. You'll need to do that with an AJAX call.

0

I have learnt that we can use only a function in "OnClick" event of a button. For this purpose i took a java script function and in this java script function. I use php insert meathod to enter data to DB fields. Isn't that way correct?

OR

What is the other way to run php code onClick event of a button in html?

Edited by akkbkht

1

While PHP can print out JavaScript, basic JavaScript cannot interact with PHP. To do so you need to use AJAX (Asynchornous JavaScript and XML), which allows JavaScript requests a file from the server without reloading the current page.

To solve your problem, read up on AJAX and how to use it to call PHP files. Once you have some code together come back here if you have any problems.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.