How to properly update a MySQL table in PHP - php

I'm trying to update a row in a table using PHP, although I'm this is not my first time, but the update function is not working.
This is the php code:
<?php
header("Access-Control-Allow-Origin: *");
$server = "localhost";
$username = "username";
$password = "dbpass";
$database = "dbname";
$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());
mysql_select_db($database, $con);
$q=mysqli_query("UPDATE `phonegap_login` SET `firstname` = 'Alhia' WHERE `reg_id` =50;");
if($q)
{
echo "success";
}
else
{
echo "failed";
}
?>
And by the way, if you try INSERT into, it will work:
$q=mysql_query("insert into `phonegap_login` (`reg_date`, `firstname`, `lastname`, `email`, `password`) values ('test', 'test', 'test', 'test', 'test')");
How do I fix it? the insert is working, updating a row is not.
I'm using a row with reg_id=50 for testing.

You're mixing mysqli_query and mysql_query. Only use mysqli_query. mysql_query is deprecated
$con = mysqli_connect($server, $username, $password) or die ("Could not connect: " . mysqli_error());
mysqli_select_db($database, $con);
$q=mysqli_query("UPDATE `phonegap_login` SET `firstname` = 'Alhia' WHERE `reg_id` =50;");
Or, Object Oriented:
$mysqli = new mysqli($server, $username, $password, $database);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$q = $mysqli->query("UPDATE `phonegap_login` SET `firstname` = 'Alhia' WHERE `reg_id` =50;");
Or, PDO:
$connectionString = 'mysql:host=' . $server . ';dbname='.$database;
try {
$db = new PDO($connectionString, $user, $pass);
} catch ( PDOException $e) {
echo "Failed to connect to Database: (" . $e->getCode() . ") " . $e->getMessage();
}
$q = $db->query("UPDATE `phonegap_login` SET `firstname` = 'Alhia' WHERE `reg_id` =50;");

Related

MySQL update via PHP

I try to update an SQL table but my code won't work. Maybe someone can take a look please.
<?php
$servername = "localhost";
$username = "user";
$password = "pwd";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE pm_videos SET `description` = REPLACE( `description` , "Instagram:", "" ";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
I get a 500 error.
When I execute this directly in the database it works very well:
UPDATE pm_videos
SET `description` = REPLACE(`description`, "Instagram:", "");
<?php
$servername = "localhost";
$username = "user";
$password = "pwd";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE pm_videos SET `description` = REPLACE( `description` , 'Instagram:', '')";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
You had a few syntax errors in your code try the above
you are messed up with quotes problem.try this changes
$sql = "UPDATE pm_videos SET `description` = REPLACE( `description` , 'Instagram:', '' )";
It's because PHP assumes you are closing a statement when you include a " inside another ". To fix this, just include single quotation inside double quotes, like:
$sql = "UPDATE pm_videos SET description = REPLACE(`description` , 'Instagram:', '');";
That's because you've improperly concatenated the strings "Instagram": and "".
Try this code:
$sql = 'UPDATE pm_videos SET `description` = REPLACE( `description` , "Instagram:", ""); ';
Please use single quote or double quote like this :
$sql = "UPDATE pm_videos SET `description` = REPLACE( `description` , 'Instagram:', '' ";

SQL Delete statement not working

include_once 'dbfunction.php';
getDbConnect();
mysqli_query("DELETE FROM crewlist WHERE id = $_GET[crew_id]") or die (mysqli_error());
echo 'Delete success';
header ('Location: crewlisting.php');
This code doesn't work, however when I replace the crew_id with the actual primary key via hard coding the delete function works
Use this (MySQLi Procedural)
In dbfunction.php should be
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
?>
and insert page should be
<?
include ("dbfunction.php"); //include db connection
$id = $_REQUEST['crew_id'];
$sql = "DELETE FROM crewlist WHERE id = '$id' ";
if (mysqli_query($conn, $sql))
{
echo "Record deleted successfully";
}
else
{
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Errors are
There is no function define in getDbConnect()
If you are confusing with 'and " then split the functions
$id = $_REQUEST['crew_id'];
$sql = "DELETE FROM crewlist WHERE id = '$id' ";
Use mysqli_query and mysqli_error in correct format
and error in mysqli_query, You are not passing connection to MySQLi
When ever database part is Finished, Close connection mysqli_close($conn);
Correct your query:
mysqli_query("DELETE FROM crewlist WHERE id ='".$_GET['crew_id']."'") or die('Error: ' . mysqli_error());

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.

PHP Error: No database selected (Hostgator.com)

I have a problem with inserting data to MySQL with PHP.
When I run this code on my Hostgator hosting I get error like this:
No database selected
Here is my code:
$dbh= mysql_connect("localhost", $username, $password);
// or die ('I cannot connect to the database because: ' . mysql_error());
if(!$dbh)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("contafe_tipster", $dbh);
//$conn = mysqli_connect($servername, $username, $password, $dbname);
//Check connection
/*
if (!$conn) {
// Connection failed return 0
//die("Connection failed " . mysqli_connect_error());
echo "1";
}
*/
//Posted parameters
$pName = $_POST['name'];
$pCountry = $_POST['country'];
$pCity = $_POST['city'];
$pLocationX = $_POST['locationX'];
$pLocationY = $_POST['locationY'];
$pDescription = $_POST['description'];
$pMobileNumber = $_POST['mobile'];
$pOtherNumber = $_POST['phone'];
$pOpens = $_POST['opens'];
$pCloses = $_POST['closes'];
$add_DB = "INSERT INTO Places (Id, Name, Country, City, LocationX, LocationY, Description, Mobile, Phone, Opens, Closes)
VALUES(NULL, '$pName', '$pCountry', '$pCity', '$pLocationX', '$pLocationY', '$pDescription', '$pMobileNumber', '$pOtherNumber', '$pOpens', '$pCloses')";
if (mysql_query($add_DB, $dbh)) {
//if success return 1
echo "0";
}
else {
//if error return -1
//echo "2";
die('Error: ' . mysql_error());
}
mysql_close($dbh);
Are you sure that db name is correct?
Try this way of connection with db:
mysql_connect ($dbhost,$dblogin,$dbpass) or die ("Can't connect to database");
mysql_select_db($db) or die ('Wrong databse!');

Mysql insert not working and not giving errors

i do not know why the following code will not work for inserting data into mysql.
if (!$link = mysql_connect('server', 'user', 'password')) {
echo '700';
exit;
}
if (!mysql_select_db('vendors', $link)) {
echo '701';
exit;
}
$sql2 = "INSERT INTO transactions (TransID, payment_status, last_name, first_name, payer_email, address_name, address_state, address_zip, address_country, verify_sign, payment_gross, ipn_track_id, business, reciver_email) VALUES ('kris', 'kris', 'kris', 'kris', 'kris','kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris')";
$result2 = mysql_query($sql2, $link);
What is wrong with the code?
php is giving no errors.
Please try not to use mysql_connect instead use mysqli_connect or PDO_MySQL read this
Also use die to find if there is any errors in your code
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
Otherwise(recommended way)-
Procedural style
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO Persons (firstname, lastname, email)
VALUES ('Happy', 'John', 'john#example.com')";
if (mysqli_query($conn, $sql)) {
echo "New Person created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
MySQLi Object-oriented style
$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 Persons (firstname, lastname, email)
VALUES ('Happy', 'John', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New Person created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
Try changing this
$result2 = mysql_query($sql2, $link);
Into this
$result2 = mysql_query($sql2, $link)or die(mysql_error());
You have to write the code like below to get the errors in your code
$result = mysql_query($sql2,$link) or die(mysql_error());
this or die(mysql_error()) will give you errors in query

Categories