Delete button not deleting all values from database - php

I created a delete button on my detail page which deletes the values which must be deleted how ever.... After deleting it's seems like the code is working because it doens't show any information on the detailpage but when I go to PHPMyAdmin it's still showing some values... I am using three tables person, address, cv after pressing delete the values from person is deleted but not from address and cv.
My delete code:
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id=$_GET['id'];
$stmt = $conn->prepare('DELETE FROM person WHERE person_id = ?');
$stmt->bind_param('s', $id);
$result = $stmt->execute();
if ($result === FALSE) {
die("Error: " . $stmt->error);
}
$conn->close();
?>
I have ON DELETE set up on CASCADE for both address_id and cv_id which has a relation with person_cv and person_address. Also I do not want to start a new topic for this small question... When I press delete it is going to a empty page called http://localhost:8080/Website/delete.php?id=(randomid) I want it to go back to the detailpage.

header("Location: http://localhost:8080/Website/detailpage");
(or whatever your detail page is called)

check foreignKey Cascade
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id=$_GET['id'];
if ($conn->query(sprintf ( "DELETE FROM person WHERE person_id = '%s' ", mysql_real_escape_string ( $id ) ) )) {
header('Location: detailpage.php');
}
else{
printf("Errormessage: %s\n", $mysqli->error);
exit();
}
$conn->close();
?>

Related

SQL with PHP variables

I'm trying to update db record using the code below. Even if the redirection is done, the record isn't being updated as it should be. any tips please?
<?php
session_start();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "Mydatabase";
$id = $_GET["session_id()"];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE leads SET first_name='hola' WHERE id = '$id'";
if ($conn->query($sql) === TRUE) {
header( "thank-you.php" ); die;
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
This one causes the problem.
$id = $_GET["session_id()"];
session_id() itself is a PHP function. No need for quoting. Just remove the quotes and it should be fine.
$id = $_GET[session_id()];

PHP Update MySQL value and redirect to a page

Im still new in PHP, I'd like help in a php example where I click a link, a value gets updated in mysql and redirect the user to a page.
Your will be greatly appreciated.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
if ($conn->query($sql) === TRUE) {
// if all ok, redirecting
header("Location: http://example.com/myOtherPage.php");
die();
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>

mysql will not insert past fourth row

I'm not exactly sure what happened but this database and the php effecting it were working just fine until it hit the fourth row and now it won't insert new records at all.
if($_POST)
{
$servername = ******;
$username = ******;
$password = ******;
$db = ******;
$conn = mysqli_connect($servername, $username, $password, $db);
mysqli_select_db($conn,$db);
$uuid = $_POST['uuid'];
$sql = "INSERT INTO uuid VALUES ('$uuid');";
mysqli_query($conn,$sql);
mysqli_close($conn);
}
I'm not sure what happened but this is the relevant code for the mysqli query.
try this
<?php
if(isset($_POST['uuid']))
{
$servername = yourServerName;
$username = username;
$password = password;
$dbname = databaseName;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$uuid = $_POST['uuid'];
$sql = "INSERT INTO tableName (columnName) VALUES ('$uuid')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
Also, I recommend using prepared statements.

Returning results from database

I am trying to do a simple SELECT to return rows of data from my database. I have a valid connection from my database so I know the issue is not there. I have ensured the names of each column are correct but it just returns 0 results.
My table inside the db is called 'user' and here is the members.php file:
<?php include 'header.php'; ?> <- this is where the db conect file is pulled in.
<?php
$sql = "SELECT id, username, email_address FROM user";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo
$row["id"];
}
} else {
echo "0 Members";
}
$conn->close();
?>
Just for ref here is my DB connection (Not the most secure i am just testing):
<?php
$servername = "localhost";
$username = "***********";
$password = "**********";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully<br><br>";
?>
you didnt select your database
$conn=new mysqli($servername, $username, $password);
this require another parameter which is your d.b name
$conn=new mysqli($servername, $username, $password,$db_name);

Simple insert into database doesn't work

I have a very simple bit of code to insert data into database.
It doesn't work. I do not get any error except cannot insert. I could connect to the database so that is not the issue.
The database has 3 fields, emailadd is the 2nd field. The other 2 are auto increment id and creation date, so also a field that on add, it will add current timestamp.
$inquiry = "INSERT INTO subscribe (emailadd) VALUES ('$myemail')";
$res = mysql_query($inquiry) or die("cannot insert");
$inquiry = "INSERT INTO `subscribe` (`emailadd`) VALUES ('$myemail')";
$res = mysql_query($inquiry,$con) or die('Not Inserted : ' . mysql_error());
Some time we need $con variable to identify the database connection, let see more check here
the mysql_query is deprecated, you need to use mysqli like so :
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>

Categories