cannot delete row from database using php - php

I am unable to delete any record from database. I cannot find any error in this.
I have deleted records from another table by just changing a little bit but here it's not working. Below is the code to apply the delete query.The name of table is from where I want to delete records but its not happening here.
deleteSupplier.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("db_kiln");
$id1 = $_GET['id1'];
$query0 = "DELETE FROM tbl_supplier WHERE sup_id='$id1'";
if(mysql_query($query0)){
echo "<script>window.open('supplier_connect.php','_self')</script>";
}
else{
echo "Not deleted";
}
?>
This is the file where I fetch data from database and have delete button against each record. When I click on the button it does not delete record and show the error message. I can't find any error I think there is a logical error in this code. Please help.
supplier_connect.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("db_kiln");
$query = "Select * from tbl_supplier";
$run = mysql_query($query);
echo "<table border='1'>
<tr>
<th>Supplier Id</th>
<th>Name</th>
<th>Contact Number</th>
<th>Quotation </th>
<th>Remove</th>
</tr>";
while($row = mysql_fetch_assoc($run)){
echo "<tr>";
echo "<td>" . $row['sup_id'] . "</td>";
echo "<td>" . $row['sup_name'] . "</td>";
echo "<td>" . $row['sup_contact'] . "</td>";
echo "<td>" .$row['sup_quotation']. "</th>";
echo '<td>Delete</td>';
echo "</tr>";
}
echo "</table>";
?>

You probably have the ID of the supplier referencing to another table, and upon DELETE, no action is set. You need to set the option 'upon DELETE: Cascade or SET NULL.' I'd go for SET NULL, else your whole reference record will be deleted as well.
EDIT: check the relationship between your contacts and quotations, see where your supplierid is going to.

Related

Populate a drop down box from sql query in a table and strip symbols

At present I have this code
$id = (isset($_GET['id']) ? $_GET['id'] : '');
$result = mysqli_query($con,"SELECT * FROM garage WHERE PlayerUID = '$id'
ORDER BY DateStored");
$row_cnt = $result->num_rows;
printf("Player has %d Vehicles stored.\n <br>", $row_cnt);
echo "<br>";
echo "<table border='1'>
<tr>
<th>Player Name</th>
<th>Vehicle</th>
<th>Date Stored</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><a href='https://www.battlemetrics.com/rcon/players?filter%5Bsearch%5D=%22".$row['PlayerUID']."%22&filter%5Bservers%5D=2191805%2C2315320%2C2384537&sort=score&showServers=true' target='_blank'>" . $row['Name'] . "
</td>";
echo "<td><a href='https://www.google.co.uk/search?q=Dayz+Epoch+".$row['DisplayName']."' target='_blank'>" . $row['DisplayName'] . "
</td>";
echo "<td>" . $row['DateStored'] . "</td>";
echo "<td><a href='deletegarage.php?del=$row[id]' Onclick='return ConfirmDelete()'>Delete</a>";
echo "</tr>";
}
echo "</table>";
Which gives me this output :
Current output
What I would like to achieve is adding another table after Vehicle field which displays the information from the database in the "Inventory" field into a dropdown list, but the current information that is in a single row looks like this:
[[[],[]],[["CinderBlocks","cinder_door_kit","cinder_garage_kit","full_cinder_wall_kit","ItemComboLock","metal_floor_kit","ItemVault","PartVRotor","ItemPole","PartGeneric","MortarBucket","ItemSledgeHandle","ItemSledgeHead","ItemTankTrap","plot_pole_kit"],[61,3,5,25,4,11,4,1,22,1,15,2,2,21,1]],[[],[]]]
so I would like to try and display each item in that list without symbols and on different lines within that dropbox if it's at all possible.
I would like it to look something like this :
What i need the dropdown to look like
I hope this provides you with enough information as to what I need and would appreciate any support!

Why is data is not printed on webpage, after being fetched from mysql database?

$retval = mysqli_query($conn,"SELECT * FROM food WHERE pnrno='$pnrno'");
if ($retval) {
echo "Your food complain has been successfully fetched";
echo "<table border='1'>
<tr>
<th>Username</th>
<th>PNR Number</th>
<th>Food Complain Status</th>
</tr>";
while($row = mysqli_fetch_array($retval))
{
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['pnrno'] . "</td>";
echo "<td>" . $row['complain_status'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "\r\n";
} else {
echo "Error: " . $retval . "<br>" . mysqli_error($conn);
}
Code inside the while loop is not getting executed(I think so), "Your food complain has been successfully fetched" this message is getting printed, table is formed, but username, pnrno, and complain_status after being fetched from database is not printed on webpage. Why is it so, please help.
Assuming the data exists, and you're sending the query the correct parameter, the likely culprit is how you're using your row variable. Since you're not telling mysqli to fetch the array in an associative manner, it's defaulting to an index.
You need to change your fetch function to:
mysqli_fetch_array($retval, MYSQLI_ASSOC)
$retval = mysqli_query($conn,"SELECT * FROM food WHERE pnrno='$pnrno'");
if ($retval) {
Your if is going to execute if the query successfully executes.
However, a query for a non-existent row will succeed, with zero rows.
You'll want to check the number of rows returned by the query, and show an error if there aren't any matching ones.

Once we create trigger in php page then do we need to create same in Mysql?

I am using Mysql for database purpose for my php code.
I have created trigger in php code as below, Now do I need to create it in mysql??
My following insert data into table, and also show content of tables. But action I performed in trigger does not make any change. Is there any problem in trigger?
Once it started working fine but after i changed table name it stopped working though I kept table name same my php page and mysql.
<html>
<body>
<?php
$id=$_POST['id'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$city=$_POST['city'];
$con=mysqli_connect('127.0.0.1:3306' ,'root','root','my_db');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql1="select * from student";
$result = mysqli_query($con,$sql1);
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Firstname</th>
<th>Lastname</th>
<th>City</th>
</tr>";
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "</tr>";
}
echo "</table>";
**$sql3 = "CREATE TRIGGER MysqlTrigger AFTER INSERT ON student FOR EACH ROW BEGIN INSERT INTO details VALUES ($id,$fname,$lname,$city);";**
mysqli_query($con,$sql3);
$sql5="INSERT INTO student (id,fname, lname, city)
VALUES
('$_POST[id]','$_POST[fname]','$_POST[lname]','$_POST[city]')";
mysqli_query($con,$sql5);
echo "1 record added";
print "<h2>After performing Trigger updated table details</h2>";
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Firstname</th>
<th>Lastname</th>
<th>City</th>
</tr>";
$sql4="select * from details";
$res = mysqli_query($con,$sql4);
while($row = mysqli_fetch_array($res,MYSQLI_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Short answer - no, you don't have to because creating it in the code also creates it in MySQL. But you have bigger problems.
Longer answer -
Triggers are part of the database, and typically you wouldn't create a trigger from code. You would create triggers the same way you create tables - create them once in MySQL and they stick around until you drop them.
Technically the code you have will work, but the CREATE TRIGGER statement will only succeed the first time it is called. In subsequent executions of that script the CREATE TRIGGER will error out because the trigger already exists. But since you aren't checking for errors, your script will probably continue on happily.
Also, the way your trigger is made, it will always insert the same record into the details table that was inserted when the trigger was created.
Finally, you have some serious security issues with your code:
You are directly using POST variables in SQL which opens you up to SQL Injection
Whatever user your site is running as probably shouldn't have permissions to execute DDL statements like CREATE TRIGGER

Showing Mysql table data in html form with php

I'm showing data from Mysql table with php inside HTML table, but i need 2 more things to accomplish to the table:
1- How to Alternate colors for table rows to use 4 different css classes, I am using now class='success' i have 3 more that i want to use and each one should apply to each table row, how to do it? any simple example like a loop or something?
2- Data showing from the oldest record in the table to the latest one, and i want to show the reverse, so the last record shows first in the html table.
My code for this table:
<?php echo "<table class='table'>
<thead>
<tr>
<th>Order#</th>
<th>Name</th>
<th>Total</th>
<th>Submitted On</th>
<th>Status</th>
</tr> </thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tr class='success'>";
echo "<td>" . $row['lite_order_id'] . "</td>";
echo "<td>" . $row['lite_item_name'] . "</td>";
echo "<td>" . $row['lite_email'] . "</td>";
echo "<td>" . $row['lite_country'] . "</td>";
echo "<td>" . $row['lite_order_total'] . "</td>";
echo "</tr>"; }
echo "</table>"; ?>
1 - Use id attribute to style individual elements. Check it out here
2 - In your MySQL query, use ORDER BY:
(I presume you have an id column here)
$query = "SELECT * FROM `yourtable` ORDER BY `id` ASC";
See here
To get reverse records you can use mysql ORDER BY clause or PHP function array_reverse()

Can you store selected sql query values in the url and then retrive them with $_GET?

I'm trying to make it so that when a users clicks one of the rows,it will take them to a new page whose link is given as the value of the row they selected and then retrieve the value with $_GET["timesub"].
Anyone know how to do this?
mysql_select_db("RRRC", $con);
$result = mysql_query("SELECT * FROM mainreq WHERE roomnum=$loc");
echo "<table border='1'>
<tr>
<th> Submitted </th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> . $row['timesub'] . </td>";
echo "</tr>";
}
echo "</table>";
Assuming that $row['timesub'] identifies a row in your data set (I doubt it), just fix your echo instruction as:
echo "<td>" . $row['timesub'] . "</td>";
Escaping the html quotes properly.
echo "<td><a href='roomdata.php?timestamp=".$row['timesub']."'>".$row['timesub']."</a></td>";
Close the outer " before the . concatenator, replace the inner " with '
A good practice is to use the row's primary key to reference your get query; but yes - this can be done.
All you have to do is store the get data into a sanitized variable, and perform the required SQL lookup / data display.
EX:
$roomnum=mysql_real_escape_string(preg_replace("/[^a-zA-Z0-9]+/", "", $_GET['roomnum']));
Now, given that "roomnum" is your primary key just look it up and display:
$result = mysql_query("SELECT * FROM mainreq WHERE roomnum='$roomnum'");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> . $row['timesub'] . </td>";
echo "</tr>";
}
echo "</table>";

Categories