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.
Related
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 ");
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"
I am trying to select values from one DB. And insert and update the result into another. This is cronjob that needs to run everyday to replicate some data from one DB into another. I know I am missing steps / correct syntax, but I hope someone can help me out.
<?php
$con_1=mysqli_connect("host","user","pw","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$con_2=mysqli_connect("host","user","pw","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con_1,"SELECT id, name FROM table GROUP BY 1,2");
$mysqli->query($con_2, "INSERT INTO `table2`(`id`, `name`) VALUES ('".$result[1]."', ".$result[2].")
ON DUPLICATE KEY UPDATE name = ".$result[2]."");
}
mysqli_close($con_1);
mysqli_close($con_2);
?>
mysqli_query returns a query object, using $result[1] doesn't make sense, you need to fetch the rows in a loop:
while($row = $result->fetch_assoc()) {
// insert result in second database
}
For other access methods check the documentation.
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
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