mysqli delete query not working - php

I'm writing a PHP script to delete a row from a MySQL database by id.
The value of the id is passed as a query string and stored in a variable.
The query runs normally, with no errors, but no rows are affected.
Here is my code, can somebody please point out what's wrong with it?
Thanks.
PHP Code:
$delete = $_GET['killthisguy'];
$sqlDel = "DELETE FROM `pba_files` WHERE id=".$delete;
$res = mysqli_query($cxn, $sqlDel);
$affRows = mysqli_affected_rows($cxn);
Database Schema:
CREATE TABLE IF NOT EXISTS `pba_files` (
`id` int(3) NOT NULL auto_increment,
`chap_id` int(2) default NULL,
`cat_id` varchar(2) character set utf8 collate utf8_unicode_ci default NULL,
`is_video` tinyint(1) default NULL,
`file_location` varchar(220) character set utf8 collate utf8_unicode_ci default NULL,
`clean_filename` varchar(116) character set utf8 collate utf8_unicode_ci default NULL,
`description` varchar(1026) character set utf8 collate utf8_unicode_ci default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=611 ;

You could try removing the ' from the "id" because it is not a string, it is an int (from the database point of view)
$delete = $_GET['killthisguy'];
$sqlDel = "DELETE FROM `pba_files` WHERE id=".$delete;
$res = mysqli_query($cxn, $sqlDel);
$affRows = mysqli_affected_rows($cxn);
Also are you sure that the "id" exists in the table?

The issue was just a careless mistake I made when setting up my access rights. DELETE was not allowed for my user on the database.
Thanks for all your help guys.

Related

Why won't this binded MySQL query run?

My code for updating user info:
if($update_stmt = $link_reg->prepare("UPDATE email_pass SET password=?, salt=?, customer_id=?, subscription_id=?, subscription_datetime=? WHERE email=?")){
$update_stmt->bind_param('ssssss', $new_password, $random_salt, $new_customer_id, $new_sub_id, $new_sub_datetime, $new_email);
if($update_stmt->errno){
echo($update_stmt->error);
}
// Execute prepared query
if($update_stmt->execute()){
// MORE CODE HERE
} else {
echo("ERROR?");
}
}
When I run it, I get no feedback. My data table doesn't update, but there's no echo message either.
Is there an error somewhere? Why won't the code execute properly?
EDIT
Here's some sample UPDATE data and the table's columns
$new_password = '532a69d8124604e33e9f45a8c9xbea92c342cbd5a3f847f770816dbd97975b2769f52a25806ead6100c1ac1a9a1a4de6b1641279a26854fba7c162caffca8e9f';
$random_salt = 'b6a1062d2c07c3aa900cbe9777d4670192f77241ad0b5ceb5f7968e3107f6d719b450d2ac37165e7827f53c2005797c985deddb9bec71724948bcd833ea72e87';
$new_customer_id = '19582601';
$new_sub_id = 'crj94x';
$new_sub_datetime = '2014-02-25 19:41:56';
$new_email = 'myemail#someemailplace.com';
The CREATE TABLE syntax:
CREATE TABLE `email_pass` (
`row_id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`password` char(128) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`salt` char(128) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`customer_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`subscription_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`subscription_datetime` datetime NOT NULL,
PRIMARY KEY (`row_id`)
) ENGINE=MyISAM AUTO_INCREMENT=239 DEFAULT CHARSET=latin1
I have tried your code and it works. You have a WHERE email=? in your code, which is populated with $new_email. May it be that you are trying to find the record to update by searching for the new email to be set rather than by the current email address (or where row_id = ...)?
I.e. you do not get any errors and nothing is updated because you do not get a match on your email = <new email> where clause.
Welp, turns out I forgot to give the MySQL user permission to UPDATE. It could only SELECT and INSERT.

Custom PHP script PDO is throwing exception 23000,1062 duplicate entry

I am working on a PHP script where I am using PDO to insert data in mySQL. I am getting an error "23000",1062,"Duplicate entry 'email#email.com-username' for key 'email' but its inserting the data in database.
So here is my PHP codes:
if(isset($_POST['email'])){
$this->db = new connect();
$this->db = $this->db->dbConnect();
$this->encryption = new Encryption();
isset($_POST['timezone']) AND $_POST['timezone'] != 'null' ? date_default_timezone_set($_POST['timezone']): date_default_timezone_set('America/Chicago');
$this->email = $_POST['email'];
$this->username = $_POST['username'];
$this->password = $this->encryption->encode($_POST['password']);
$this->dTime = date("Y-m-d H:i:s");;
$this->sessionKey = $_POST['key'];
$this->country = $_POST['country'];
$this->region = $_POST['uregion'];
$this->browser = $_POST['browser'];
$this->ip = $_POST['accessFrom'];
$regMessage = array('error'=>false);
try{
$query = "INSERT INTO `users` (
id, email, uname, password, regtime, sessionkey, country, region, browser, ip
) VALUES (
(SELECT MAX(id) + 1 FROM `users` AS `maxId`), :email, :uname, :password, :regtime, :sessionkey, :country, :region, :browser, :ip
)";
$register = $this->db->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
if($this->sessionKey === $_SESSION['token']){
$register->bindParam(':uname', $this->username);
$register->bindParam(':email', $this->email);
$register->bindParam(':password', $this->password);
$register->bindParam(':regtime', $this->dTime);
$register->bindParam(':sessionkey', $this->sessionKey);
$register->bindParam(':country', $this->country);
$register->bindParam(':region', $this->region);
$register->bindParam(':browser', $this->browser);
$register->bindParam(':ip', $this->ip);
$register->execute();
if($register->rowCount() > 0){
$regMessage = array('error'=>false);
}else{
$regMessage = array('error'=>true);
}
}else{
throw new PDOException ('Error');
}
}
catch(PDOException $e){
//this is where I am getting error so I am echoing pdo exception error
$regMessage = array('error'=>$e);
}
header('Content-Type: application/json');
echo json_encode($regMessage);
}else{
header('Location: /');
}
At the error, it is showing me duplicate entry of emailid + username for key email which looks like email#email.com-username
But in data base, I am getting email id only in email column and username only in username column.
So can any one tell me whats wrong in my codes?
My users table structure is
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(250) CHARACTER SET utf8 NOT NULL,
`uname` varchar(20) CHARACTER SET utf8 NOT NULL,
`password` varchar(100) CHARACTER SET utf8 NOT NULL,
`regtime` datetime NOT NULL,
`sessionkey` varchar(10) CHARACTER SET utf8 NOT NULL,
`country` varchar(25) CHARACTER SET utf8 NOT NULL,
`region` varchar(25) CHARACTER SET utf8 NOT NULL,
`browser` varchar(25) CHARACTER SET utf8 NOT NULL,
`ip` varchar(16) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`,`uname`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
So can anyone tell me where and what is wrong?
Thank you for helping me.
The phrasing in the error message: 'email#email.com-username' for key 'email' directly corresponds to your unique key UNIQUE KEY 'email' ('email','uname'). With that line, you are creating a compound key, which you can think of as an invisible column in the index that is comprised of email-uname. There will not be a column added to your table with this format, and you are seeing the expected behavior that email and uname are treated separately in the table and together for the key.
If you want to test over and over again with the same email and username combo, you'll need to delete that row every time. Without doing this, the error you are seeing is exactly what I would expect to see if you are POST-ing the same data over and over again.
I want to also mention that you have (appropriately) specified that your id column is AUTO_INCREMENT, but then you are calculating the value manually. I would like to discourage you from doing this, and instead use NULL as the insert value. MySQL will use the correct key value in this column, and you will avoid the potential for key collision if you ever had two of these things executing at the same exact moment.
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(250) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`uname` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`password` varchar(100) CHARACTER SET utf8 NOT NULL,
`regtime` datetime NOT NULL,
`sessionkey` varchar(10) CHARACTER SET utf8 NOT NULL,
`country` varchar(25) CHARACTER SET utf8 NOT NULL,
`region` varchar(25) CHARACTER SET utf8 NOT NULL,
`browser` varchar(25) CHARACTER SET utf8 NOT NULL,
`ip` varchar(16) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
this is the solution.

Is it possible to set 2 separate unique fields using Redbean ORM

I am using the redbeanPHP ORM and mysql. I have the following table:
CREATE TABLE `mast` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`note` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`geolocation` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`location` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`zip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`state` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`app` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UQ_84a93b55f688c94f73092dba1b9590c41a92cbf5` ('app')
) ENGINE=InnoDB AUTO_INCREMENT=98 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I want to insert records into the 'mast' table providing they are unique with respect to both of the 2 fields listed above. In other words if either 'geolocation' or 'app' is a duplicate, I don't want to insert the associated record.
I am using following php code to create the 2 unique fields:
$mast= R::dispense('mast');
$mast->setMeta("buildcommand.unique" , array(array('geolocation')));
$mast ->import($resultsarray);
$mast->setMeta("buildcommand.unique" , array(array('app')));
$id = R::store($mast); // DUMMY BEAN
A unique index is only being created for the 'app' field . Is there a way to set them both as unique in redbean?
From the documentation it should work like the following:
$mast = R::dispense('mast');
$mast->setMeta("buildcommand.unique.0", array('geolocation', 'app'));
$mast->import($resultsarray);
$id = R::store($mast);
In your code you just overwrote the value for build command.unique.

Am I getting an SQL syntax error

CREATE TABLE IF NOT EXISTS `contracts` (
`contractId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT,
`title` varchar(20) CHARACTER SET utf8 NOT NULL,
`contractText` text CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
`contractState` tinyint(1) NOT NULL COMMENT '1-Nepatvirtinta, 2- patvirtinta, 3- panaikinta, 4- atmesta',
PRIMARY KEY (`contractId`),
UNIQUE KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci AUTO_INCREMENT=15 ;
INSERT INTO `contracts` (`contractId`, `username`, `title`, `contractText`, `date`, `contractState`) VALUES
CREATE TABLE IF NOT EXISTS `users` (
`username` varchar(32) CHARACTER SET utf8 NOT NULL,
`password` varchar(32) CHARACTER SET utf8 NOT NULL,
`email` varchar(20) CHARACTER SET utf8 NOT NULL,
`usergroup` tinyint(1) NOT NULL COMMENT,
`name` varchar(32) CHARACTER SET utf8 NOT NULL,
`lastname` varchar(32) CHARACTER SET utf8 NOT NULL,
`state` tinyint(4) NOT NULL COMMENT,
PRIMARY KEY (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci;
When running this query on the SQL server, I am getting the following error:
MySQL said: Documentation
#1064 - 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 '
title varchar(20) CHARACTER SET utf8 NOT NULL,
contractText text CHAR' at line 13
why this is coming up?
You did not add a comment to the username column.
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT ,
add one here---------------------------------------^
or remove the COMMENT keyword
in this line
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT 'comment',
add some comment in for COMMENT or Remove Comment. Below is working code.
CREATE TABLE IF NOT EXISTS `contracts` (
`contractId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT 'comment',
`title` varchar(20) CHARACTER SET utf8 NOT NULL,
`contractText` text CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
`contractState` tinyint(1) NOT NULL COMMENT '1-Nepatvirtinta, 2- patvirtinta, 3-
panaikinta, 4- atmesta',
PRIMARY KEY (`contractId`),
UNIQUE KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci AUTO_INCREMENT=15 ;
Just fix your table creation to add explicit comment or remove COMMENT keyword if it is not needed.
Try modify it to become:
CREATE TABLE IF NOT EXISTS `contracts` (
`contractId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT 'add explicit comment her',
`title` varchar(20) CHARACTER SET utf8 NOT NULL,
`contractText` text CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
`contractState` tinyint(1) NOT NULL COMMENT '1-Nepatvirtinta, 2- patvirtinta, 3- panaikinta, 4- atmesta',
PRIMARY KEY (`contractId`),
UNIQUE KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci AUTO_INCREMENT=15 ;
Remove comment from this line:
`username` varchar(32) CHARACTER SET utf8 NOT NULL , //COMMENT

mysql php Cannot take foreign key value

Hello I have a problem with my php code.. there are 3 tables for a recruitment system.
CREATE TABLE IF NOT EXISTS `members` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`password` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`cpassword` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`email` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`role` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`uid`)'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=60 ;
The candidate table :
CREATE TABLE IF NOT EXISTS `candidate` (
`fullname` varchar(35) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`webpage` varchar(150) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`tel` int(35) NOT NULL,
`nationality` varchar(35) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`position` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`interviewed` varchar(30) NOT NULL DEFAULT 'No',
`rating` varchar(30) NOT NULL,
`c_id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
PRIMARY KEY (`c_id`),
KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=135 ;
The academic table :
CREATE TABLE IF NOT EXISTS `academic_candidate` (
`degree` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`exp_years` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`comment1` varchar(300) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`proposed_positions` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`research_years` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`comment2` varchar(300) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`department` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`a_id` int(25) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
`c_id` int(11) NOT NULL,
PRIMARY KEY (`a_id`),
UNIQUE KEY `id` (`a_id`),
KEY `uid` (`uid`),
KEY `c_d` (`c_id`),
KEY `c_id` (`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ;`
-- Constraints for table academic_candidate
ALTER TABLEacademic_candidate
ADD CONSTRAINTacademic_candidate_ibfk_1FOREIGN KEY (uid) REFERENCESmembers(uid) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINTacademic_candidate_ibfk_2FOREIGN KEY (c_id) REFERENCEScandidate(c_id) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table candidate
ALTER TABLEcandidate
ADD CONSTRAINTcandidate_ibfk_1FOREIGN KEY (uid) REFERENCESmembers(uid) ON DELETE CASCADE ON UPDATE CASCADE;
--
Now, I use this query in order to store the values in the table academic_candidate
session_start();
$query = "SELECT * FROM academic_candidate WHERE degree = '$degree'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count > 0){
echo "You ALready complete the form </br>";
header("Location:../candidate/candidate_index.php");
}
else{
$degree =($_POST['degree']);
$exp_years = ($_POST['exp_years']);
$comment1 = ($_POST['comment1']);
$proposed_positions = ($_POST['proposed_positions']);
$research_years=($_POST['research_years']);
$comment2=($_POST['comment2']);
$department=($_POST['department']);
$uid=($_SESSION['uid']);
$query1 = "INSERT INTO academic_candidate
(degree,exp_years,comment1,proposed_positions,research_years,comment2,department,uid,c_id)
SELECT
'$degree','$exp_years','$comment1','$proposed_positions','$research_years','$comment2','$department','$uid','$c_id'
FROM candidate
WHERE uid='$uid' AND c_id='$c_id' ";
$result = mysql_query($query1);
if(!$result){
echo "Error";
die (mysql_error());
}
else{
header("Location:../candidate/view_application.php");
}
}
My problem is that stores all the values on the table academic_candidate table but the c_id is 0 . What can I do in order to take the candidate.c_id?
You're passing literal values to your INSERT INTO statement.
Consider the difference:
SELECT 'bar' FROM foo;
SELECT bar FROM foo;
Here's a demo on SQLFiddle.
Looking at your query:
SELECT '$degree'
Is not the same as:
SELECT degree
One of these is simply echoing whatever value you pass in, the other is actually selecting a column from the DB.

Categories