Delete only certain things from database - php

I got this php script from w3schools and I was wondering if its possible to only delete the last name and leave the first name which is peter?
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"DELETE FROM Persons WHERE LastName='Griffin'");
mysqli_close($con);
?>
source:
http://www.w3schools.com/php/php_mysql_delete.asp

Yes, just do
UPDATE Persons
set LastName = ''
WHERE
id = ?
or alternatively set LastName = NULL

Related

I have a domain 'itcircle.net' which is hosted in 'freehosting.com'. Now i want to create a table in mysql

My database name is "itcircle_s" user is "itcircle_ss"
Now I want to create a table name "simple_tb".
But I failed. My code is like below:
$con=mysql_connect("localhost","root","")or die(mysql_error());
$db=mysql_select_db("s",$con)or die(mysql_error());
if(mysql_query("CREATE TABLE IF NOT EXISTS simple_tb(id int(255) NOT NULL AUTO_INCREMENT,n varchar(40) ,PRIMARY KEY ( id ));",$con)){
echo"TABLE created";
} else {
echo"Error creating database: ".mysql_error();
}
mysql_close($con);
You have a problem on your script because you are using user 'root' to connect to it .
Check below : replace my_password with your password.
<?php
$con = mysqli_connect("localhost","itcircle_ss","my_password","itcircle_s");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql=mysqli_query($con," your query goes here ");

MySql Select Command returning null

I have a simple php code which enters value into MySql database and then retrieves it and displays it. However when retrieving it always return null and Empty Set is echoed everytime. Can someone please help.
I am using WAMP Server.
Database name is trial and name of table is People. It has 2 attributes: name and email id
Following is my code:
$con=mysqli_connect("localhost","root","");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
mysqli_query($con,"INSERT INTO People VALUES ('xyz', 'abc#zzz.com')");
echo "Insertion Success";
$result = mysqli_query($con,"SELECT * FROM People");
if($result == null)
echo "Empty Set";
else
while($row = mysqli_fetch_array($result))
{
echo $row['name'] . " " . $row['emailid'];
echo "<br>";
}
mysqli_close($con);
?>
You should select a database after using mysqli_connect but before any queries are done:
$con=mysqli_connect("localhost","root","");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
mysqli_select_db($con, "databasename");
//query processing here..
You should check if record is inserted change your code to
if( mysqli_query($con,"INSERT INTO People VALUES ('xyz', 'abc#zzz.com')") === FALSE){
echo mysqli_error();
}
first always check if you query executed write by executing it inside if():
if(!mysqli_query("your query"))
{
mysqli_error();
}
second i think your query is not executing because you cant name you table with a capital letter so it should be "people" not "People"

Loop until the value is null

i need to loop the process of deleting if there is an id in the database that the user input. can someone help me about this?
//my php code
$ppnum=$_POST['pnum'];
while ($ppnum !== null)) {
$con=mysqli_connect("","","","");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"DELETE FROM tbldd WHERE pnum ='$ppnum'");
mysqli_close($con);}
maybe you want this? This assume $_POST['pnum'] is comma separated list of ids.
$ppnum=$_POST['pnum'];
$con=mysqli_connect("","","","");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"DELETE FROM tbldd WHERE pnum in ($ppnum)");
mysqli_close($con);
but you MUST check $_POST['pnum'] against sql injection attack.

PHP MySQL - Error: No Database selected

I am trying to read and write to a database. Here is the code I have so far:
$mysql = mysqli_connect("example.com", "johndoe", "abc123"); // replace with actual credidentials
$sql = "CREATE DATABASE IF NOT EXISTS dbname";
if (!mysqli_query($mysql, $sql)) {
echo "Error creating database: " . mysqli_error($mysql);
}
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($mysql);
$mysql = mysqli_connect("example.com", "johndoe", "abc123", "dbname"); // replace with actual credidentials
$sql = "CREATE TABLE IF NOT EXISTS Users(ID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(ID), username CHAR(15), password CHAR(15), email CHAR(50))";
if (!mysqli_query($mysql, $sql)) {
echo "Error creating table: " . mysqli_error($mysql);
}
$sql = "INSERT INTO Customers(username, password, email) VALUES(" . $username . ", " . $password . ", " . $email . ")";
if (!mysqli_query($mysql, $sql)) {
echo "Error: " . mysqli_error($mysql);
}
mysqli_close($mysql);
However, when I try to run it, it has an error:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' , )' at line 1
Could anybody tell me how to fix this?
Please check syntax of mysqli, it takes 4 parameters.You also have to provide database name.
$link = mysqli_connect("myhost","myuser","mypassw","my_db");
You're missing database in mysqli_connect() call
$link = mysqli_connect("hostname","username","password","database") or die("Error " . mysqli_error($link));
Obviously the answer is in your error. You didn't select any database. When using this function mysqli_connect specify the database you want to connect to.
Here is the syntax of the function: http://www.w3schools.com/Php/func_mysqli_connect.asp .
1) Create your database outside of your application
2) Specify mysqli_connect with the database you want to select.
You can also use another function called mysqli_select_db . You can find the sytanx here : http://www.w3schools.com/php/func_mysqli_select_db.asp .
As already stated in the comment, you will also have to replace : "example.com" with your ip address, if you are running locally replace it with 127.0.0.1:3306 , if you didn't change the port when you installed your mysql database / "johndoe" with your database account, you can change that to "root" / "abc123" with your root account password DEFAULT : "" .
Good luck !
First check mysqli_select_db if it returns false then create database.
try like this:
$mysql = mysqli_connect("example.com", "johndoe", "abc123") or die(mysqli_connect_error()); // replace with actual credidentials
if (!mysqli_select_db($mysql,'hardestgame_accounts')) {
$sql = "CREATE DATABASE IF NOT EXISTS hardestgame_accounts";
if (!mysqli_query($mysql, $sql)) {
echo "Error creating database: " . mysqli_error($mysql);
}
}
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "CREATE TABLE IF NOT EXISTS Users(ID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(ID), username CHAR(15), password CHAR(15), email CHAR(50))";
if (!mysqli_query($mysql, $sql)) {
echo "Error creating table: " . mysqli_error($mysql);
}
mysqli_close($mysql);
here is a good answer: Php mysql create database if not exists
Try using mysql_select_db in between the database connection and the table creation.
Also, mysql_ is deprecated, please use mysqli_ instead
Use ` backticks for MYSQL reserved words...
your table name is reserved word for MYSQL...
Change your table name.

Inserting Data to a Database into the correct row using the session User Id

Does anyone know how I would Insert data into a mysql database and in the correct row using the user Id in the session It will insert into the database but not in the correct row Here is the code I have tried.
<?php
$con=mysqli_connect("localhost","","","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
':user_id' => $_SESSION['user']['id'],
mysqli_query($con,"INSERT INTO login (code, link)
VALUES ('You Found a Secret Game!', '/12283719823838hdhj/') WHERE id = :user_id ");
mysqli_close($con);
?>
I tried using ':user_id' => $_SESSION['user']['id'],but It didn't work.
I think you need to do this
<?php
$con=mysqli_connect("localhost","","","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO login (code, link)
VALUES ('You Found a Secret Game!', '/12283719823838hdhj/') WHERE id = " .
$_SESSION['user']['id']);
mysqli_close($con);
?>
If you look at the SQL you are pushing through I think you will find it is trying to check where id = :user_id rather than the value of :user_id

Categories