Why won't this binded MySQL query run? - php

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.

Related

Set names and collation UTF8 does not works in SimpleXML or MySQL

I am gettin XML data which header has utf8 setted:
<?xml version="1.0" encoding="utf-8"?>
I download it using CURL and it is downloaded correctly with UTF8 chars.
I parse that downloaded xml files using SimpleXML using this code:
<?php
class XMLParser{
public function __construct(){
}
public function parseFile($path){
$xml = '';
if(!is_null($path)){
$file = file_get_contents($path);
if(!is_null($file)){
$xml = new SimpleXMLElement($file);
}
else{
echo 'there is no file';
}
}
else{
echo 'Wrong path!';
}
return $xml;
}
}
?>
And send it to database calling PDO and stored procedures, connect script has defined utf8:
$this->PDO->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
Each table has utf8_general_ci
But in table are chars like this:
León de Huánuco - Cienciano
UPDATE!
CREATE TABLE code:
CREATE TABLE IF NOT EXISTS `jcjdkdtaur`.`fixtures` (
`Id` INT(32) NOT NULL,
`UpdatedDate` varchar(100) DEFAULT NULL,
`DateTime` datetime NULL DEFAULT NULL,
`Date` date NULL DEFAULT NULL,
`Time` time NULL DEFAULT NULL,
`Status` VARCHAR(40) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL DEFAULT NULL,
`League` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL DEFAULT NULL,
`LeagueID` INT(11) DEFAULT NULL,
`SportID` tinyint DEFAULT 1,
`SportName` varchar(30) DEFAULT 'football',
`Cup` varchar(20) DEFAULT NULL,
`Country` VARCHAR(60) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL DEFAULT NULL,
`EventName` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL DEFAULT NULL,
`HomeTeam` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL DEFAULT NULL,
`HomeTeam_Id` INT(11) NULL DEFAULT NULL,
`AwayTeam` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL DEFAULT NULL,
`AwayTeam_Id` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`Id`,`HomeTeam_Id`,`AwayTeam_Id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
INSERT STORE PROCEDURE I CALL IT FROM PHP:
DROP PROCEDURE IF EXISTS insupd_Fixtures;
DELIMITER //
CREATE PROCEDURE insupd_Fixtures(IN p_id int(32), IN p_updated varchar(100), IN p_datetime datetime, IN p_date date,
IN p_time time, IN p_status varchar(40),IN p_league varchar(255),IN p_leagueid int(11), IN p_sportid tinyint, IN p_sportname varchar(30),
IN p_cup VARCHAR(20), IN p_country VARCHAR(60),IN p_eventname varchar(255),IN p_hometeam varchar(255),IN p_hometeam_id int(11),
IN p_awayteam varchar(255),IN p_awayteam_id int(11)
)
BEGIN
INSERT INTO fixtures(`Id`,`UpdatedDate`,`DateTime`,`Date`,`Time`,`Status`,`League`,`LeagueID`,`SportID`,`SportName`,`Cup`,`Country`,`EventName`,
`HomeTeam`,`HomeTeam_Id`,`AwayTeam`,`AwayTeam_Id`)
VALUES
(p_id,p_updated, p_datetime, p_date, p_time, p_status,p_league, p_leagueid, p_sportid, p_sportname, p_cup,p_country,
p_eventname,p_hometeam,p_hometeam_id,p_awayteam,p_awayteam_id)
ON DUPLICATE KEY UPDATE
Id=p_id,
UpdatedDate = p_updated,
DateTime=p_datetime,
Date=p_date,
Time=p_time,
Status=p_status,
League=p_league,
LeagueID = p_leagueid,
SportID = p_sportid,
SportName = p_sportname,
Cup=p_cup,
Country=p_country,
EventName=p_eventname,
HomeTeam=p_hometeam,
HomeTeam_Id=p_hometeam_id,
AwayTeam=p_awayteam,
AwayTeam_Id=p_awayteam_id;
END //

Inserting data from form to mysql database

I am using PHP to send data from a form to mysql database, when I use this code:
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
phone VARCHAR(60) NOT NULL,
password VARCHAR(60) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
email VARCHAR(50),
pelak VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
reg_date TIMESTAMP
)";
it works perfectly, but when I use:
$field_table = $_POST['phone'];
$sql = "CREATE TABLE '{$field_table}' (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
phone VARCHAR(60) NOT NULL,
password VARCHAR(60) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
email VARCHAR(50),
pelak VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
reg_date TIMESTAMP
)";
it create table in phpmyadmin but gives an error:
"#1146 - Table 'username.table_name' doesn't exist"

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.

mysqli delete query not working

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.

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