I need some help understanding the correct way to mix variables with strings like this. I have tried every configuration I can think of and I keep getting an error.
<?php
include('connect.php');
foreach($_GET['item'] as $key=>$value) {
mysql_query("UPDATE userprojectlist SET category_display_order = '$key' WHERE category_id = '$value' ");
}
?>
Notice: Undefined index: item in updatedb.php on line 3
Warning: Invalid argument supplied for foreach() in pdatedb.php on line 3
Is item an array in the url like item[]? If it's not that's the reason you get that error, you cannot iterate through a non-iterator object!
Also get rid of the single quotes around the values if they are numeric and use string concatenation,
"UPDATE userprojectlist SET category_display_order = " . $key . " WHERE category_id = " . $value . ";"
Notice: Undefined index: item in updatedb.php on line 3
This error is saying that there's no such variable as $_GET['item'] defined. You probably did not pass $_GET['item'] to this page.
Warning: Invalid argument supplied for foreach() in pdatedb.php on line 3
Thus because of the previous error, you get this, as $_GET['item'] is not an array.
The error has nothing to do with the SQL code.
mysql_query('UPDATE userprojectlist SET category_display_order = ' . $key . ' WHERE category_id = ' . $value );
or
mysql_query("UPDATE userprojectlist SET category_display_order = $key WHERE category_id = $value" );
This is assuming both variables are integers.
Related
I keep getting the following errors. The $query produces a 1300 result list. When I run echo $query I get the following MySQL error:
[25-Aug-2016 21:38:32 America/New_York] PHP Warning: mysql_fetch_row() expects parameter 1 to be resource, null given in song.php on line 285
[25-Aug-2016 21:38:32 America/New_York] PHP Notice: Undefined variable: song_hash in song.php on line 292
$query = "select " . $query_data . " from " . $query_tables . " where " . $query_where;
//echo $query;
$result = mysql_query($query,$database);
while($row = mysql_fetch_row($result)){
$key = $row[0];
$song_hash[$key] = ($song_hash[$key] + 1);
}
$largest = max($song_hash);
In order to help you the contents of $query_data, $query_tables and $query_where needed. You can also print the error with mysql_error(). More likely you will be able to find out the solution from that output.
http://php.net/mysql_error
when you run echo query and you get those errors it simply means you don't have a query..
try hardcoding the query for debugging,
also you need to initialize your song_hash array
I'm having trouble updating my postgressdb using update clause where itemid = "$_get['itemid'];
here's my sql code but it returns Warning: pg_query(): Query failed: ERROR:
$sql="UPDATE tbl_item SET itemname='".$_POST['ItemName']."', highqntythreshold='".$_POST['HQThreshold']."', lowqntythreshold='".$_POST['LQThreshold']."', qntyperunit='".$_POST['QPUnit']."', itemtype='".$_POST['IT']."', description='".$_POST['Description']."', WHERE itemid='". $_GET['itemid'] . "';";
$iteminfo = pg_query($sql);
and it also returns "Warning: pg_affected_rows() expects parameter 1 to be resource, boolean given in D:\Wamp\wamp\www\Php\CTea\UpdateItem.php on line 303"
if(pg_affected_rows($iteminfo)==1)
{
$msg = "Successfully added new Item, ".ucfirst($_POST['ItemName'])."!";
}
else
{
$msg = "Error: in saving Item data!...";
}
i think i messed up something but can't figure it out where and what i messed up.
The problem is (at least) in this part:
$_POST['Description']."', WHERE itemid='". $_GET['itemid'] . "'
There is a comma before the where, so you want:
$_POST['Description']."' WHERE itemid='". $_GET['itemid'] . "'
In general, though, you should just print out the query string after variable substitution. About 98% of the time, the error is obvious and you can fix it quickly.
i got this error message while running my Php codeigniter project :
A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for mtree::insert_to_right(), called in
C:\AppServ\www\News\application\controllers\bcontroller.php on line 20
and defined
Filename: models/mtree.php
Line Number: 28
this is my model function :
function insert_to_right($row, $direct_id) {
$left_parent = $this->get_right_parent($direct_id);
$row->members_direct_id = $left_parent;
$this->db->insert('table1', $row);
$sql = "INSERT INTO table2 (child, parent)
VALUES (" . $this->db->escape($row->members_id) . ", " . $this->db->escape($left_parent) . ")";
$this->db->query($sql);
}
function get_right_parent($id) {
return $id;
}
Since the function insert_to_right($row, $direct_id) requires two arguments, either you can pass in those two variables when you call them, or you can assign default values for it like insert_to_right($row = '....', $direct_id = '...'). I don't think you can just pass in any default variable as this would insert it into the database, so I would recommend you pass in the argument from the controller.
I got this error message when running PHP Code Igniter project:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$members_id
Filename: models/mtree.php
Line Number: 33
In line 33 ($row->members_id) refer to field in my database
VALUES (" . $this->db->escape($row->members_id) . ", " . $this->db->escape($left_parent) . ")";
My model function is:
function insert_to_right($direct_id='members_direct_id') {
$left_parent = $this->get_right_parent('members_id');
$row->members_direct_id = $left_parent;
$row->members_spillover_id = $left_parent;
$this->db->insert('ajmatrix_members_table', $row);
$sql = "INSERT INTO ayman_mot (child, parent)
VALUES (" . $this->db->escape($row->members_id) . ", " . $this->db->escape($left_parent) . ")";
$this->db->query($sql);
}
Your code don't have the members_id property in row.
$row variable have only 2 properties:
$row->members_direct_id = $left_parent;
$row->members_spillover_id = $left_parent;
Somewhere in your Model where you have the db select statement, for example:
$this->db->select('field1, field2, field3, field4');
try and replace it with:
$this->db->select('*');
Worked out for me, after battling it out for weeks through forums.
Though my initial mistake was putting the statement like this:
$this->db->select('field1', 'field2', 'field3', 'field4');
~ Mistakes are the ones we make ~ (speaking for myself here, don't hit me ^_^ )
Best Regards
You're not defining $row->members_id in your code - elsewhere you're referencing a members_direct_id - is that what you meant to use perhaps?
<?php
mysql_connect("localhost","root","");
mysql_select_db("hftwmvirtualdb");
$Booknum = mysql_real_escape_string($_POST['Booknum']);
$Chapternum = mysql_real_escape_string($_POST['Chapternum']);
$Versenum = mysql_real_escape_string($_POST['Versenum']);
$sql = mysql_query("SELECT `VERSETEXT` FROM `booktable` WHERE `BOOKID` = $Booknum AND `CHAPTERID` = $Chapternum AND `VERSENO` = $Versenum");
echo mysql_error();
while($row=mysql_fetch_assoc($sql));
print(json_encode($row));
mysql_close();
?>
I am trying to use posted data from an android application to trigger a query and retrieve the results from the mysql database. The Table has 4 columns, and I'm trying to retrieve the value in the third column by defining the values in the first 3 columns. Each time i clicked the button, I get the parsing error to find out my PHP script was not processing the SQL query. When running the scriptthrough the browser I get the messages:
Undefined index: Booknum in C:\wamp\www\GetVerse.php on line 4
Undefined index: Chapternum in C:\wamp\www\GetVerse.php on line 5
Notice: Undefined index: Versenum in C:\wamp\www\GetVerse.php on line 6
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 'AND CHAPTERID = AND VERSENO =' at line 1
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\GetVerse.php on line 9.
I understand i get the warning messages 1-3 is because I did not submit the post data but the latter I don't know how to fix as I have tried using the correct syntax, I tried removing "=" for "like" and that failed also. What is the problem?.
The undefined index errors are, as you specified, occurring because you did not submit the post data. This, in turn, is causing the variables $Booknum, $Chapternum, and $Versenum to be empty.
With the empty variables, the MySQL query is being generated with a WHERE clause like:
WHERE `BOOKID` = AND `CHAPTERID` = AND ...
The missing values are causing invalid MySQL, hence your error. Additionally, as you've specified (in a comment) that the POST-values are strings (and not integers which is what I would have assumed based on their usage and names), you have to wrap the values in quotes in your MySQL query too. If you do not wrap the values in quotes, even valid strings may cause the query to fail.
To fix this, try something like:
$Booknum = isset($_POST['Booknum']) ? mysql_real_escape_string(trim($_POST['Booknum'])) : null;
$Chapternum = isset($_POST['Chapternum']) ? mysql_real_escape_string(trim($_POST['Chapternum'])) : null;
$Versenum = isset($_POST['Versenum']) ? mysql_real_escape_string(trim($_POST['Versenum'])) : null;
if (!empty($Booknum) && !empty($Chapternum) && !empty($Versenum)) {
$sql = mysql_query("SELECT `VERSETEXT` FROM `booktable` WHERE `BOOKID` = '" . $Booknum . "' AND `CHAPTERID` = '" . $Chapternum . "' AND `VERSENO` = '" . $Versenum . "'");
echo mysql_error();
while($row=mysql_fetch_assoc($sql));
print(json_encode($row));
mysql_close();
}
This will verify that the values are properly set - if not, they will be set to null. If all three values are not empty, via PHP's empty(), your query will be executed.
This is what your SQL query will look like when the variables are substituted in:
SELECT `VERSETEXT` FROM `booktable` WHERE `BOOKID` = AND `CHAPTERID` = AND `VERSENO` =
When the variables contain no content (as they won't if you submit no data), the query is meaningless: the syntax is malformed.
Check whether the data is posted before doing the query. Moreover, it will also profit you to start using parameterised queries (using MySQLi or PDO) for security and convenience.
The "undefined index" messages you're getting are because those variables are not set. Check that you're actually posting those to the script.
The empty variables are why your query is wrong and you get an error.
Consider using PDO as the "mysql_" commands are deprecated. You should check your inputs before passing them to the query. isset() will work for that.
CHeck whether the Post data is coming or not, undefined index it is because, there is no data for the variables you have used. SO first verify it and then execte the SQL query.
if(isset($_POST['Booknum']) && isset($_POST['Chapternum']) && isset($_POST['Versenum']))
{
$Booknum = mysql_real_escape_string($_POST['Booknum']);
$Chapternum = mysql_real_escape_string($_POST['Chapternum']);
$Versenum = mysql_real_escape_string($_POST['Versenum']);
$sql = mysql_query("SELECT `VERSETEXT` FROM `booktable` WHERE `BOOKID` = $Booknum AND `CHAPTERID` = $Chapternum AND `VERSENO` = $Versenum");
echo mysql_error();
while($row=mysql_fetch_assoc($sql));
print(json_encode($row));
}
else
{
echo "No post data";
}