Undefined index. database column name undefined - php

I've tried a lot of ways to execute this and it still says my column is undefined. I thought I didn't link it to my database correctly or something but even after searching for solutions here I still get the same error. I'm super newbie in coding especially in PHP and this problem had literally gone for days.

you are getting undefined for column because you have not check each of those with isset() or empty()
you should do like
$menuname = ( !empty($_POST['menu_Name']) )?mysqli_real_escape_string($query, $_POST['menu_Name']):'';
or
$menuname = ( isset($_POST['menu_Name']) )?mysqli_real_escape_string($query, $_POST['menu_Name']):'';
also looks your query is wrong.
it should be like
INSERT into mytable (mycolums) VALUES ('myvalues')
where
mytable is the name of table
mycolums can be name of one or more columns with comma saperated
and
myvalues can be value of each colums like 'col1val','col2val, ...
don't forgot to vote up if it helped you

Related

PHP echo of SQL query

I've a problem with my code. I can't wrap my head around what's wrong with the code or where I fail.
I basically want to calculate the average of multiple numbers from my database from one column.
/*data calculation*/
$calculation = "SELECT AVG(current) FROM offset_items";
$calculation_result = mysqli_query($connect, $calculation);
if($average = mysqli_fetch_assoc($calculation_result))
{echo "Average: ".$average["current"]."<br>";}
The problem here is, that I get only a blank displayed after the "Average: ".
But if I remove the AVG from the query, I get all results listed of that DB column. The DB contains data, which I import in the same script, as well as displaying them for testing.
I get the following error, which is the line of the echo:
Notice: Undefined index: current in C:\MAMP\htdocs\Offset_Items.php on line 50
I think the problem here is the mysqli_fetch_assoc, but I'm not sure.
Digging through the multiple topics about that "Undefined index" didn't solve my problem. Even when I declare the variable before as NULL.
I'm out of touch with programming, but currently picked it up again to code some tools for work. So if you guys could help me, I would be really thankful.
The PHP code that attempts to print the result reads:
$average = mysqli_fetch_assoc($calculation_result);
echo "Average: ".$average["current"]."<br>";
but the query that generates the result set does not contain any column named current.
Use an alias for the expression AVG(current) in the SELECT clause to get a column in the result set having the desired name:
SELECT AVG(current) AS current FROM offset_items

php mysqli syntax for mixing variable with text in a field

hi and thank you in advance for any help you can give!
i'm iffy on syntax with variables in queries.
i have a database table with a column called 'iltr' and a column called 'refline'.
the 'refline' column is filled with text data like this: '1LINE1' '1LINE2' '1LINE3' '2LINE1' '2LINE2' '2LINE3' etc etc etc.
i want to use a variable that relates to the first digit of the data in the 'refline' column to update another column.
in the example below i'm trying to set 'iltr'= 'Y' where 'refline' is '2LINE3' by using a variable for the "2" digit in '2LINE3' instead of the "2" digit itself, and i don't even know if this kinda thing can be done.
this code syntax below does not work specifically in the WHERE segment where i try to mix the variable with the actual text in the column:
$slot=2;
$sql = "UPDATE overall SET iltr='Y' WHERE refline='$slotLINE3'";
i hope you can forgive my inexperience. thanks again and have an awesome day!
Your code is trying to substitute a variable named $slotLINE3. You need to use a delimiter to end the variable name.
$sql = "UPDATE overall SET iltr='Y' WHERE refline='{$slot}LINE3'";

Running a MySQL query using a string in php

Answer found (syntax): The column name of my string had to be encased in backticks " ` " as they contained spaces. Note that this means that the majority of this post has no relevance to the issue. The code has been corrected in case someone wants to do something similar.
So, I am doing a foreach loop to assign a value (1/0) to non-static columns in my database (it needs to support addition/deletion/editing of columns). I am using $connectionvar->query($queryvar); to do my queries which worked fine up until now when I'm trying to use a custom built string as $queryvar in order to change the column name to a variable within the loop. I've been outputting this string through echo and it looks exactly like my functional queries but somehow doesn't run. I've attempted to use eval() to solve this but to no avail (I feel safe using eval() as the user input is radio buttons).
Here's the loop as well as my thought processes behind the code. If something seems incoherent or just plain stupid, refer to my username.
foreach($rdb as $x) { //$rdb is a variable retrieved from $_POST earlier in the code.
$pieces = explode("qqqppp", $x); //Splits the string in two (column name and value) (this is a workaround to radio buttons only sending 1 value)
$qualname = $pieces[0]; //Column name from exploded string
$qualbool = $pieces[1]; //desired row value from exploded string
$sql = 'UPDATE users SET '; //building the query string
$sql .= '`$qualname`';
$sql .= '=\'$qualbool\' WHERE username=\'$profilename\''; //$profilename is retrieved earlier to keep track of the profile I am editing.
eval("\$sql = \"$sql\";"); //This fills out the variables in the above string.
$conn->query($sql); //Runs the query (works)
echo ' '.$sql.' <br>'; //echoes the query strings on my page, they have the exact same output format as my regular queries have.
}
}}
Here's an example of what the echo of the string looks like:
UPDATE users SET Example Qualification 3='1' WHERE username='Admin2'
For comparison, echoing a similar (working) query variable outside of this loop (for static columns) looks like this:
UPDATE users SET profiletext='qqq' WHERE username='Admin2'
As you can see the string format is definitely as planned, yet somehow doesn't execute. What am I doing wrong?
PS. Yes I did research this to death before posting it, as I have hundreds of other issues since I started web developing a month ago. Somehow this one has left me stumped though, perhaps due to it being a god awful hack that nobody would even consider in the first place.
You need to use backticks when referring to column names which have spaces in them. So your first query from the loop is outputting as this:
UPDATE users SET Example Qualification 3='1' WHERE username='Admin2'
But it should be this:
UPDATE users SET `Example Qualification 3`='1' WHERE username='Admin2'
Change your PHP code to this:
$sql = 'UPDATE users SET `'; // I added an opening backtick around the column name
$sql .= '$qualname`'; // I added a closing backtick around the column name
$sql .= '=\'$qualbool\' WHERE username=\'$profilename\'';
Example Qualification 3 : Is that the name of your Mysql Column name ?
You shouldnt use spaces nor upper / lower case in your columnname.
Prefere : example_qualification_3
EDIT :
To get column name and Comment
SHOW FULL COLUMNS FROM users

Session variable: save an auto increment from a sql field next a insertion

I want to save in a session variable on php an auto increment value field from one SQL table, just next to do the insert on that table. Why it appears empty? What should I do?
Because I check my sql statement on the database and everything is correct, all of that and have values.
$usersetdataregister="INSERT INTO `questionnaire`.`tbluserset`(`UserIDFKPK`, `QuestionSetIDFKPK`) VALUES ('$UserId','0')";
mysql_query($usersetdataregister);
$query_usersetID= "SELECT tbluserset.UserSetIDPK
FROM tbluserset
INNER JOIN (SELECT US.UserIDFKPK, MAX(US.Timestamp) AS MaxTimestamp
FROM tbluserset AS US
WHERE US.UserIDFKPK ='$UserId'
GROUP BY US.UserIDFKPK) AS USL ON tbluserset.UserIDFKPK = USL.UserIDFKPK AND tbluserset.Timestamp = USL.MaxTimestamp";
$row_UserSetIDPK = mysql_query($query_usersetID);
$UserSetID= $row_UserSetIDPK['UserSetIDPK'];
$_SESSION['UserSetIDPK']= $UserSetID;
Next to do this i want use the value on a query statement and UserSetIDPK appears empty.
I think you forgot to start your session.
session_start();
$_SESSIONS['UserSetIDPK'] = $UserSetID;
You should also check what $row_UserSetIDPK is returning. If it's empty then there is something wrong with your query, or he can't find any results in your database.
If it's not empty, you should see if $row_UserSetIDPK['UserSetIDPK'] is the right way to get your value.
to debug try this:
var_dump($row_UserSetIDPK);die();
Hope this helps a bit, 'cause your post is quite a mess! :-)

Trying to translate a MySQL query into a PHP query

I'm still kinda new to PHP and MySQL and I've tried so many times to do this and I just can't figure out how.
I have a query that returns the results I want in PHPMyAdmin as a straight MySQL query but I'm trying to get this to generate on a webpage using a PHP $query and I just can't get the syntax right.
this is the working MySQL query:
SELECT fk_toon_no, fk_actor_no, actor_no, actor FROM cartoon_characters,
characters WHERE fk_toon_no=50 HAVING fk_actor_no=actor_no;
The kicker is that I also want to have a variable $new_toon_id as the = for the WHERE statement, so, something like: (but only displaying the row as I will eventually plug this into a table and know how to do that fine)
WHERE fk_toon_no=$new_toon_id
fk_actor_no is the foreign key of the cartoon_characters table to the primary key actor_no in the characters table.
I'm trying to get it so that I can print out every character associated with a particular cartoon so it would look something like
(toon id) (character id #) (character name)
($fk_toon_no) (actor_no) (actor)
3 5 Eisenhower
3 9 Nixon
3 12 Uncle Sam
Any help would be greatly appreciated. I think I've included all the relevant information but if I forgot anything please ask.
I'm in desperate need of help. Thanks!!
$query=<<<HERE
SELECT fk_toon_no, fk_actor_no, actor_no, actor FROM cartoon_characters,
characters WHERE fk_toon_no='50' HAVING fk_actor_no=actor_no;
HERE;
$send=mysql_qyery($query);
while($row = mysql_fetch_assoc($send))
{
echo $row["fk_toon_no"];
echo "<br />"
echo $row[fk_actor_no];
}
This should do the trick.

Categories