MySQLi & mysql_real_escape_string() Giving me a bunch of errors - string

Im having problem with my UPDATE code., since i'm new at php, im trying to figure out what's been the prolem with my UPDATE command here's my line of code. My insert and delete commands are good to go but this one gives me a headache for 4 hours now.
<?php
include 'includes/conn.php';
if($_POST){
//write query
$sql = "UPDATE
member
SET
fname = ?,
lname = ?,
mname = ?,
email = ?,
address = ?,
gender = ?,
contact = ?,
email = ?,
username = ?,
password = ?
WHERE
mem_id = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param(
'sssssssssi',
$_POST['fname'],
$_POST['lname'],
$_POST['mname'],
$_POST['email'],
$_POST['address'],
$_POST['gender'],
$_POST['contact'],
$_POST['username'],
$_POST['password'],
$_POST['mem_id']
);
if($stmt->execute()){
echo "User was updated.";
$stmt->close();
}else{
die("Unable to update.");
}
}
$sql = "SELECT
fname, lname, mname, email, address, gender, contact, username, password, mem_id
FROM
member
WHERE
mem_id = \"" . $mysqli->real_escape_string($_GET['mem_id']) . "\"
LIMIT
0,1";
$result = $mysqli->query( $sql );
$row = $result->fetch_assoc();
extract($row);
$result->free();
$mysqli->close();
?>
hope you guys could help o by the way it gives me this error
Undefined index: mem_id in C:\xampp\htdocs\prjTheVillagePlaygroup\execute_editmenu.php on line 134
Warning: extract() expects parameter 1 to be array, null given in C:\xampp\htdocs\prjTheVillagePlaygroup\execute_editmenu.php on line 141

You have
email = ?
listed twice in the update statement. Delete the second one.
Note that your binding does not have to change. It will have the correct number of parameters after the deletion.
Also, you are using $_GET['mem_id'] where it should likely be $_POST['mem_id'] - all your other variables are set with $_POST
With regard to Warning: extract() expects parameter 1 to be array, null given, this is due to the query failing.
Check for the $result to make sure the query succeeded and what the error is:
$result = $mysqli->query( $sql );
if (!$result)
printf("Error: " . mysqli->error); // or use die("Error: " . mysqli->error)
}
$row = $result->fetch_assoc();
extract($row);

Related

Different approach for pagination - LIMIT when using SQLSRV

I'm trying to make an instant pagination using SQLSRV and PHP, I have successfully did this using MySQL but unable to do so when using SQL Server as it does not support LIMIT.
I have the following codes working in MySQL and I wanted to apply the same thing in sqlsrv but since this is not possible, I'm looking forward in creating a different approach(code) to achieve this, can someone give me an idea or a walkthrough to make this happen please, thanks in advanced.
if(isset($_POST['page'])):
$paged=$_POST['page'];
$sql="SELECT * FROM `member` ORDER BY `member`.`member_id` ASC";
if($paged>0){
$page_limit=$resultsPerPage*($paged-1);
$pagination_sql=" LIMIT $page_limit, $resultsPerPage";
}
else{
$pagination_sql=" FETCH 0 , $resultsPerPage";
}
$result=sqlsrv_query($sql.$pagination_sql);
Try the following code, I hope you find it helpful
$paged = filter_input(INPUT_POST, 'page', FILTER_SANITIZE_NUMBER_INT);
//Initialize these values
$Table = 'your_tbl_name'; //Table name
$IndexColumn = 'pk_col_name'; //Primary key column
$resultsPerPage = '10'; //Page size
$Where = ''; //Optional WHERE clause, may leave empty
$Order = ''; //Optional ORDER clause, may leave empty
$top = ($paged>0) ? $resultsPerPage * ($paged-1) : 0 ;
$limit = 'TOP ' . $resultsPerPage ;
$pagination_sql = "SELECT $limit *
FROM $Table
$Where ".(($Where=="")?" WHERE ":" AND ")." $IndexColumn NOT IN
(
SELECT $IndexColumn FROM
(
SELECT TOP $top *
FROM $Table
$Where
$Order
)
as [virtTable]
)
$Order";
$result=sqlsrv_query($conn, $pagination_sql);

How to make case sensitive query with nodejs + pg

How to make case sensitive query with nodejs + pg
I want to select column content == 'a#gmail.com',
but it seems become select column == 'a#gmail.com'?
[error: column "a#gmail.com" does not exist]
code
var userEmail = 'a#gmail.com';
var query = 'SELECT EXISTS(SELECT * FROM "User" WHERE "Email" = "'+userEmail+'")';
dbClient.query(query, function(error, result) {
...
For use binding parameters you must number it begining with $1 (then $2 and so), then put the parameters in an Array:
var query = 'SELECT EXISTS(SELECT * FROM "User" WHERE "Email" = $1)';
dbClient.query(query, [userEmail], function(error, result) {
Always pass the parameters in an array. Is most secure.
Remember do not pass a function to query if you have a very big table unless you want to read all the table before returns the control to de function. Else you can use the "on" event or use a promise way (like https://www.npmjs.com/package/pg-promise-strict)
This doesn't have anything to do with case. The problem is that you're putting the email address in double-quotes, and in (most varieties of) SQL double-quotes indicate a column name or table name. That's why the error message says column "a#gmail.com" does not exist.
Use single-quotes around values:
var userEmail = 'a#gmail.com';
var query = 'SELECT EXISTS(SELECT * FROM "User" WHERE "Email" = \'' + userEmail + '\')';
Ideally, though, you should just use parameter binding so you don't have to worry about quoting values at all. When you use string concatenation to build SQL queries you very often open yourself up to SQL injection attacks.

how to get value from datareader

I want to get a values from datareader, but there is an error called
"No data exists for the row/column".
this is my code
//select the group where status is active
OleDbCommand com2 = new OleDbCommand("select group from tblBillConfig where status=1 group by group",con);
OleDbDataReader dr2 = com2.ExecuteReader();
//int i = Convert.ToInt32(dr2);
string ii = dr2["group"].ToString();
MessageBox.Show(ii);
please anyone can help?
If your query doesn't return any record then you get that message.
You need to check if there are rows returned and then try to read them....
By the way, I am pretty sure that the word GROUP is a reserved keyword fow everyt SQL database system. To use it you should enclose it in square brakets (but that could be different for every database system)
OleDbCommand com2 = new OleDbCommand("select [group] from tblBillConfig " +
"where status=1 group by [group]",con);
OleDbDataReader dr2 = com2.ExecuteReader();
// This will load the first row, so you could get its value
if(dr2.Read())
{
string ii = dr2["group"].ToString();
MessageBox.Show(ii);
}
else
{
MessageBox.Show("Query doesn't return any rows");
}

PDO get name of selected columns as object

How can I fetch selected data to an object and retrieve the selected columns name with PDO?
Now, I'm fetching to a Member object in a loop, and manually add each column to the object by accessing it from the "fetch array". Is there a way to automatically get the column names that's been seleced?
$sql = "SELECT member.name, member.email FROM member WHERE id = :id";
$param = array(':id' => $id);
$stmt = $this->m_db->select($sql, $param);
$ret = new MemberArray();
while ($f = $stmt->fetch()) {
$member = new Member($f['name'], $f['email']);
$ret->add($member);
}
return $ret;
I found the solution in PDOStatement::bindColumn and PDO::FETCH_OBJ

Searching phpbb's 'topic_title' via MYSQL php, but exact match doesn't work

$sql = sprintf( "SELECT topic_title
FROM `phpbb_topics`
WHERE `topic_title` LIKE '%%%s%%' LIMIT 20"
, mysql_real_escape_string('match this title')
);
Which I run this query in phpMyAdmin the results are: (correct)
match this title
match this title 002
But when I run that same MYSQL query in PHP I get: (incorrect)
match this title 002
I have also tried MATCH AGAINST with the same result with both php and phpMyAdmin:
$sql = "SELECT topic_title
FROM phpbb_topics
WHERE MATCH (topic_title)
AGAINST('match this title' IN BOOLEAN MODE)";
The whole block of code im using to search with:
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("phpbb") or die(mysql_error());
$query = "match this title";
$query = "SELECT topic_title
FROM phpbb_topics
WHERE MATCH (topic_title)
AGAINST('$query' IN BOOLEAN MODE)";
// Doesn't work (these 2 both give the same result "match this title 002" and no the "match this title")
// $query = "SELECT * FROM `phpbb_topics`
// WHERE `topic_title`
// LIKE '%$query%'
// LIMIT 0, 30 "; // Doesn't work
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$topic_title = $row['topic_title'];
echo "$topic_title";
}
Any idea as to what i'm doing wrong?
I'v been searching all over the place and have found next to no help :(
The problem is that after you execute your query you fetch the first row, do nothing with it, enter the loop by fetching the second row and start printing results..
If you remove the first $row = mysql_fetch_array($result), (directly after $result = mysql_query($query) or die(mysql_error());) you should be fine.
Another comment; If you echo a variable you don't have to put any qoutes around it. And in the way you're doing it now, you won't get a newline between the results so you might want to change that line to echo $topic_title . "<br>";

Resources