I need to delete a data on database by using PHP code, i have written the code but there is some error message.
here is the php code(del.php):-
<!DOCTYPE html>
<html>
<body>
<?php
$conn = mysql_connect('localhost', 'root','');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM register WHERE name='' ";
if ($conn->query($sql) === TRUE)
{
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
</body>
</html>
The database name is 'selva' and the table name is 'register', in database the file names are "Name,Email,Contact,Address", i need to delete the name or email or contact . how to delete!!
//this will delete the whole row:
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "your_password"; // add your pw from the here
$dbname = "selva";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//this will delete the whole row
$sql = "DELETE FROM register WHERE 1 ";
if ($conn->query($sql) === TRUE)
{
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
</body>
</html>
//this will delete only the COLUMNS you set here:
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "your_password"; // add your pw from the here
$dbname = "selva";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "ALTER TABLE register DROP Name, DROP Email; DROP Contact; ";
if ($conn->query($sql) === TRUE)
{
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
</body>
</html>
hope it will help you :)
Related
I tried this but it didn't work, please i need support, i want a situation when i click on the delete button, i will truncate and empty the table.
This is the del.php file
<?php
$servername = "localhost";
$username = "root";
$password = "admin101";
$dbname = "school";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_REQUEST['delete'])) {
$myquery = mysql_query("TRUNCATE TABLE student");
if ($myquery) {
echo "Deleted successfully";
} else {
echo "Error: " . $myquery . "<br>" . $conn->error;
}
}
?>
The HTML below.
<form method="post" action="del.php">
<input type="submit" value="Delete" name="delete">
</form>
I've been trying to figure out how to insert data into mySQL database for a long time. When I try to insert data, it returns "no database selected". I'm not too sure what's wrong with the code, could someone check it out?
<?php
$servername = "localhost";
$database= "learnsc2_ts";
$username = "learnsc2_admin";
$password = "Ts#123";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
echo "Connection successful";
}
$query = "INSERT INTO users(fname, lname) VALUES ('Owen',
'Feng')";
mysqli_query($conn, $query);
if (mysqli_query($conn, $query)) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . mysqli_error($conn);?>
Make sure your database name is correct.
i tested it in my local, It's work Just fine.
$servername = "localhost";
$database= "test";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "INSERT INTO users(fname, lname) VALUES ('Owen',
'Feng')";
$query = mysqli_query($conn, $query);
if ($query) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . mysqli_error($conn);
}
You forgot to add database name
$conn = new mysqli($servername, $username, $password, $database);
I figured it out. Something was wrong with the old username I was using. After changing to a new username and database, it worked out!
I am trying to send a data from android studio, but I am getting lines under the table instead of assigning data.
Dont know where I am gone wrong.Plz help me.Thanks in advance.
This is my PHP code
add_employee
<?php
include('connection.php');
if (isset($_POST["name"])){
$emp_name = $_POST["name"];
echo $emp_name;
echo "is your name";
}
else{
$emp_name = NULL;
echo "POST filename is not assigned";
}
$success = 0;
$status = "Active";
$sqli = "INSERT INTO `employee` (`emp_name`) VALUES ('$emp_name')";
if(mysqli_query($conn,$sqli)){
$success=1;
}
$response["success"]=$success;
die(json_encode($response));
mysqli_close($conn);
?>
Connection.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(!$conn) {
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($conn,'student');
?>
You have so many errors in your code. no db name, no proper query definition. you can use this simple code:
Connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "slim";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Inserting employee code:
<?php
if (isset($_POST["name"])){
$emp_name=$_POST["name"];
echo $emp_name;
echo "is your name";
}
else{
$emp_name = null;
echo "POST filename is not assigned";
}
$success=0;
$status="Active";
$sql = "INSERT INTO employee (name)
VALUES ('$emp_name')";
if ($conn->query($sql) === TRUE) {
$success=1;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
It's working and easy to understand for you.
I have this code in 2.html page
<a href='1.php' >click me</a>
I want, when i click on this link, this lines run in 1.php page
<html>
<head>
<meta charset="utf-8">
<title>
my page
</title>
</head>
<body>
<?php
include('simple_html_dom.php');
$html=file_get_html($url);
foreach($html->find('a') as $element){
$html2=$element->href;
}
I want to store $html2 in database then it be returned to my first page. be easy on me. any help will be appreciate.
Please add the below code into the bracket after $html2
$html2=$element->href;
$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 = "INSERT INTO MyDataTable (htmlcontent)
VALUES ($html2)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
header("Location:myfirst.php");
die();
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();
?>