i'm trying to delete a row from database and i`m using the following code. When i submit the page reloads and if i check the db the row is still there. No error, no nothing. What can i do to solve this?
HTML
<div class="delete_row">
Sterge
<form method="post" action="">
*<input type="text" name="id_col" Placeholder="Id-ul coloanei"><br>
<input type="submit" name="submit1" value="Sterge">
</form>
</div>
PHP
$id_stergere=isset($_POST["id_col"]);
$submitcheck2=isset($_POST["submit1"]);
if($submitcheck2 && $id_stergere !==0 ){
$sql = "DELETE FROM evenimente WHERE ID_even=$id_stergere";
$result = query_mysql($sql);
}
if (isset($_POST["id_col"]))
$id_stergere=$_POST["id_col"];
if (isset($_POST["submit1"]))
$submitcheck2=$_POST["submit1"];
//additional check:
// if (!is_numeric($id_stergere)) die('there was a problem');
// if (!$submitcheck2) die('there was a problem');
$sql = "DELETE FROM evenimente WHERE ID_even=".$id_stergere;
$result = mysql_query($sql);
query_mysql?
Use this for your php :
error_reporting(E_ALL ^ E_NOTICE);
if(isset($_POST['submit1'])){
$id_stergere=$_POST["id_col"];
$submitcheck2=$_POST["submit1"];
if($submitcheck2 && $id_stergere){
$sql = "DELETE FROM evenimente WHERE ID_even=$id_stergere";
$result = mysql_query($sql);
}
}
You should be using mysqli instead though.
Use this instead of the $sql = and $result = :
$link = mysqli_connect("localhost","db","password","user") or die("Error " . mysqli_error($link));
$query= "DELETE FROM evenimente WHERE ID_even=$id_stergere";
$result= mysqli_query($link,$query);
To get the numrows use this:
$numrows = $result->num_rows;
For a fetch_array:
while($row = $result->fetch_array()){
$var= $row['field'];
}
UPDATE: Add error_reporting(E_ALL ^ E_NOTICE); to the top of php script.
Related
How can I submit a table row data to another table using an input field in PHP and MYSQL?
HTML
<form method="post" action="post.php">
<input type="number" name="code" placeholder="Code..."/>
<input type="submit" name="submit" value="Submit"/>
</form>
Post.php
if (isset($_POST['submit'])) {
$code = $_POST['code'];
$getCode = "SELECT * FROM products WHERE code=$code";
mysqli_query($connection, $getCode);
if ($_POST['code'] == $code) {
$migrating = "INSERT INTO managment(price) VALUES ($price) SELECT
price FROM products";
mysqli_query($connection, $migrating);
header("location: index.php");
}
}
What is wrong with my code?
The syntax is :
INSERT INTO managment(price) SELECT price FROM products
Maybe you want add a WHERE clause :
INSERT INTO managment(price) SELECT price FROM products WHERE code=$code
Note: in your code, $_POST['code'] == $code doesn't make sense, because of $code = $_POST['code'] earlier.
I also suggest you to have a look to How can I prevent SQL injection in PHP? to secure your queries.
Your code updated (untested) :
if (isset($_POST['submit'])) {
$code = $_POST['code'];
$getCode = "SELECT * FROM products WHERE code=$code";
$result = mysqli_query($connection, $getCode);
if (!$result) die("Error: " . mysqli_error($connection));
$row = mysqli_fetch_assoc($result);
if ($row['code'] == $code) {
$migrating = "INSERT INTO managment(price)
SELECT price FROM products WHERE code=$code";
$result2 = mysqli_query($connection, $migrating);
if (!$result2) die("Error: " . mysqli_error($connection));
header("Location: index.php");
exit;
}
}
I have connection, and I wan't delete one record by choosing ID (option have value of row ID in db)
<form class="form">
<?php
require "connect.php";
$select = $_POST['del_zaint'];
if(isset($_POST['Del'])){
$$que = "DELETE FROM `zainteresowania` WHERE `zainteresowania`.`id` = '".$select."'";
mysqli_query($db, $que);
}
mysqli_close($db);
?>
<span class="main-page__info">Usuń rekord zainteresowań.</span>
<select name="del_zaint">
<option disabled selected>Wybierz rekord do usunięcia</option>
<?php
require "connect.php";
$que = "SELECT * from zainteresowania";
$wynik = mysqli_query($db, $que);
while($row = mysqli_fetch_array($wynik)){
echo "<option value=".$row['id'].">"."[".$row['id']."] ".$row['zainteresowanie']."</option>";
}
mysqli_close($db);
?>
</select>
<input name="Del" type="submit" value="Usuń">
</form>
Nothing is done by this :/ I choose option and after clicking submit with name = Del, it won't work, just reset to normal position. (Adding informations to db and showing from it works well)
Try This
You miss the method in the form tag.
Also, I Change the SQL query
$que = "DELETE FROM `zainteresowania` WHERE id = '".$select."'";
Below code is working on my system. You have to use the SQL injection for secure
$db->real_escape_string($_POST['del_zaint'])
instated of delete the record, Make a column like status in the database and change the status 0 or 1.
You can refer the site for deleting the record
https://www.w3schools.com/php/php_mysql_delete.asp
<?php
include('db/connection.php');
if(isset($_POST['Del'])){
echo $select = $db->real_escape_string($_POST['del_zaint']);
$que = "DELETE FROM `zainteresowania` WHERE id = '".$select."'";
if (mysqli_query($db, $que)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($db);
}
mysqli_close($db);
}
?>
<form class="form" method="POST" action="#">
<span class="main-page__info">Usuń rekord zainteresowań.</span>
<select name="del_zaint">
<option disabled selected>Wybierz rekord do usunięcia</option>
<?php
$que = "SELECT * from zainteresowania";
$wynik = mysqli_query($db, $que);
while($row = mysqli_fetch_array($wynik)){
echo "<option value=".$row['id'].">"."[".$row['id']."] ".$row['zainteresowanie']."</option>";
}
mysqli_close($db);
?>
</select>
<input name="Del" type="submit" value="Usuń">
</form>
Please change this to:
require "connect.php";
$select = $_POST['del_zaint'];
if(isset($_POST['Del'])){
$que = "DELETE FROM `zainteresowania` WHERE `zainteresowania`.`id` = $select";
mysqli_query($db, $que);
}
mysqli_close($db);
I have had cases where PHP only notices variables if they are included directly in double quotes and but not single quotes. The other option is to use string concatenation so that PHP knows where the variable is.
require "connect.php";
$select = $_POST['del_zaint'];
if(isset($_POST['Del'])){
$que = "DELETE FROM `zainteresowania` WHERE `zainteresowania`.`id` = '".$select."'";
mysqli_query($db, $que);
}
mysqli_close($db);
The main issue is you have not set method="post" with your form tag. So set it there and give it a try.
A suggestion:
Chage your query like this:
$que = "DELETE FROM `zainteresowania` WHERE `zainteresowania`.`id` = '".$select."'"; // Check the changes I made around $select.
I've got my code which is searching my database and table for a certain condition but when I search it doesn't return any result. I've looked at a few tutorials and cant find the issue. Any help is appreciated. I know the code is outdated and I should be using mysqli. I will be changing this when the issue is rectified.
<?php
$output = NULL;
if(isset($_POST['submit'])){
mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db("first_db") or die("can not connect");
$search = $mysql->real_escape_string($_POST['search']);
$resultSet = $mysql->query("SELECT * FROM voulunteer WHERE Name LIKE '%search%'");
if($resultSet->num_rows > 0){
while($rows = $resultSet->fetch_assoc())
{
$StaffStatus = $rows['StaffStatus'];
$name = $rows['Name'];
$output = "Staff Status: $StaffStatus<br/>name: $Name<br/><br/>";
}
}else{
$output = "No results";
}
}
?>
<form method-"POST">
<input type="TEXT" name"search" />
<input type="SUBMIT" name="submit" value="Search" />
</form>
Your query is written wrong
instead of
$resultSet = $mysql->query("SELECT * FROM voulunteer WHERE Name = LIKE '%search&'");
try this
$resultSet = $mysql->query("SELECT * FROM voulunteer WHERE Name LIKE '%search%'");
edit: added nogad's comment about changing the & to %
Try the query in phpmyadmin first. If there's an error in the query it will tell you
Im just beginner and a cant understand why my code generates same identical rows after inserting it. please help me.
here is my html code:
<form id="sing" action="signup.php" method="POST">
<input type="text" name="first" placeholder="სახელი"> <br>
<input type="text" name="uid" placeholder="მომხმარებელი"> <br>
<input type="password" name="pwd" placeholder="პაროლი"> <br>
<button type="submit">რეგისტრაცია</button>
</form>
php:
include 'dbh.php';
$first = $_POST['first'];
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "INSERT INTO user (first, uid, pwd)
VALUES ('$first', '$uid', '$pwd')";
$result = mysqli_query($conn, $sql);
if (!mysqli_query($conn, $sql)) {
echo "maica joo";
} else {
echo "kaia";
}
That's because mysqli_query($conn, $sql) is called twice, change the code to:
$result = mysqli_query($conn, $sql);
if (!$result) {
echo "maica joo";
} else {
echo "kaia";
}
you are running mysqli_query() twice!! So it creates 2 identical rows.
// first time it insert a row
$result = mysqli_query($conn, $sql);
// now you run it again and it creates that row again
if (!mysqli_query($conn, $sql)) {
echo "maica joo";
} else {
echo "kaia";
}
Amend this to
// first time it insert a row
$result = mysqli_query($conn, $sql);
// now test $result to see if the query failed
if (!$result) {
echo "maica joo";
} else {
echo "kaia";
}
You are running the query twice with the two mysqli_query.
Change it to:
$result = mysqli_query($conn, $sql);
if (!$result) {
I am having a problem calling random number from the database.
Here is the my link:
<form method="post" action="pages/test.php?id=<?php echo "$id[0]";?>&ran=<?php echo "$ran";?>">
<input type="submit" name="Submit" value="Click here" class="button">
</form>
And here is my second page code:
<?php
if (!isset($submit)) {
//database connection here
$id = $_GET['id'];
$ran = $_GET['ran'];
$query2 = "SELECT * FROM table WHERE id='$id' AND ran='$ran'";
$result2 = mysql_query ($query2) or die ('Could not run query: ' . mysql_error());
$info = mysql_fetch_array ($result2);
?>
When I call for example firstname or lastname it woks okay. But $ran is returning empty. Am I missing something.
Whilst I don't totally understand your request, one notable error I can see is below.
Your second page should be like this
<?php
if (isset($_POST)) {
//database connection here
$id = $_POST['id'];
$ran = $_POST['ran'];
$query2 = "SELECT * FROM table WHERE id='$id' AND ran='$ran'";
$result2 = mysql_query ($query2) or die ('Could not run query: ' . mysql_error());
$info = mysql_fetch_array ($result2);
}
?>