i might be doing some idiot mistake, but i could not figure that out. i have some values coming from html and wanna insert into mysql db. problem is, the very same query does not work in regular php file (that includes other queries), but when i try on an independent php file, it does. here is a sample of the code:
$sql15="insert into body
(Article_ID, Article_Title)
values
('$article_id', '".$_POST['Article_Title']."') ";
mysql_query($sql15);
as i mentioned, the very same code works when i just copy this snippet to a new php file, and it works smoothly.. as you see, there are 20+ insert with the same php, because there are 25+ tables, but data is not much. first 14 query and following 7 queries do work by the way.
do you have any ideas?
There are some things to check and do.
Sanitize user input:
"('$article_id', '".mysql_real_escape_string($_POST['Article_Title'])."')";
You might also want to check if the value is what you expect.
Is your $article_id correct for column Article_ID?
Are your table and column names correct?
Check for errors:
$res = mysql_query($sql15);
if (!$res)
echo mysql_errno($link) . ": " . mysql_error($link);
Show us you complete query:
echo $sql15;
First of all i would suggest you to write your insert query like below
$sql15="insert into body SET Article_ID = '$article_id', Article_Title = '".$_POST['Article_Title']."'";
echo $sql15;
mysql_query($sql15);
so that each time when you add new column to database it would be easy for u to change insert query. echo your query and see it in browser. in it seems to o.k then copy it and paste it in SQL section under your phpmyadmin (see you are choosing proper database) and run it. if one row inserted successfully then your query is alright.
I hope this would help you a little.
$sql15="insert into body
(Article_ID, Article_Title)
values
('$article_id', '".$_POST['Article_Title']."') ";
mysql_query($sql15) or die(mysql_error());
use like this u will be get the error. then u will be find the issue
I think using mysql_real_escape_string may solve your problem.I also recommend you to store your form data in a string.
$article_title= mysql_real_escape_string($_POST['Article_Title']);
$sql15="insert into body
(Article_ID, Article_Title)
values
('$article_id', '$article_title') ";
mysql_query($sql15) or die(mysql_error());
Related
I am trying to retrieve an auto-incremented value that is created upon insertion. This ID value will be used in a SELECT statement later, so it would be nice to know what it is when I insert instead of writing another query. The problem is that I don't know where OUTPUT clause outputs it. The query is in the form of a PHP string variable.
I'm new to SQL and PHP, so I'm not really sure what to try. I just need some help understanding.
$query = "INSERT INTO Table1 (orderNumber, revisionNumber, creationDate...)"
. " OUTPUT Inserted.ID"
. " VALUES (blah, blah, blah...)";
execute_query($query);
Hi I have two tables that I need to insert into.
the issue is that the first table has an ID field that is automatically generated and I need this field in the second query
members (table1):
|id|name|eyeColour|
assignedMembers (table 2):
|id|memberID|groupID|
I am currently using the below:
$addMember = $dbHandle->prepare("INSERT INTO members(name,date) VALUES(?,?)");
$addMember->bind_param("ss",$name,$eyeColour);
$addMember->execute();
$getID = $dbHandle->("SELECT id from members where name = ? LIMIT 1");
$getID->bind_param("s",$name);
$getID->execute();
$getID->bind_param($MID);
$assignMember= $dbHandle->prepare("INSERT INTO assignedMembers memberID,groupID) VALUES(?,4)");
$assignMember->bind_param("i",$MID);
$assignMember->execute();
This fails at the $assignMember->bind_param(); after troubleshooting I noticed that the $MID variable is empty.
it seems as though the row from the first INSERT is not added before the execution of the next statement is there a way to force this?
Thank you for taking the time to read this post, any help would be greatly appreciated
mysqli:$insert_id is what you are looking for.
$addMember = $dbHandle->prepare("INSERT INTO members(name,date) VALUES(?,?)");
$addMember->bind_param("ss",$name,$eyeColour);
$addMember->execute();
$id = $dbHandle->insert_id;
I think you should use
$getID->bind_result($MID);
$getID->fetch();
instead of
$getID->bind_param($MID);
Due to usage of '...->bind_param' I assume, you use MySQLi.
Check out: mysqli_insert_id — Get the ID generated in the last query
Anyone see what is wrong with this line of code? using php/mysql
$sqlInsert="INSERT INTO sched_trades_proposed (id,originalDate,originalUserid,originalRot,original_sched_main_id,proposedDate,proposedUserid,proposedRot,proposed_sched_main_id,timeStampedProposal,randomHash) VALUES('','".$originalDate."',".$_SESSION[userid].",$originalRotation,$original_sched_main_id,'".$proposedDate."',$proposedRad,$proposedRotation,$proposed_sched_main_id,UNIX_TIMESTAMP(),'".$randomHash."')";
fire bug says "no element found" in jquery-1.8.2.js (line 7209, col 314) and points to this INSERT statement. I don’t see anything wrong with the insert statement. I realize fire bug isn't server side. I am still trying to solve the error though. If I comment out this line of code I get no error.
the surrounding code is:
$randomHash=sha1(rand(1,1000));
$sqlInsert="INSERT INTO sched_trades_proposed (id,originalDate,originalUserid,originalRot,original_sched_main_id,proposedDate,proposedUserid,proposedRot,proposed_sched_main_id,timeStampedProposal,randomHash) VALUES('','".$originalDate."',".$_SESSION[userid].",$originalRotation,$original_sched_main_id,'".$proposedDate."',$proposedRad,$proposedRotation,$proposed_sched_main_id,UNIX_TIMESTAMP(),'".$randomHash."')";
echo '<p>'.$sqlInsert;
$resultInsert=mysql_query($sqlInsert);
when I manually insert the INSERT statement into mySQL it gives no errors...But I am getting while trying through the web page?
Here is an example with data in the outputted INSERT statement which all looks good:
INSERT INTO sched_trades_proposed (id,originalDate,originalUserid,originalRot,original_sched_main_id,proposedDate,proposedUserid,proposedRot,proposed_sched_main_id,timeStampedProposal,randomHash) VALUES('','2013-01-10',10,7,710,'2013-01-14',3,19,723,UNIX_TIMESTAMP(),'f33f7ae89c2c6ab8e29a3cb0a97bb1f9456aacba')
FYI: the original id is auto incremented, so I insert a '' as the first column which is kosher.
Firebug says so because you echoed it to the client, and the client HTML parsing is confused because of that. Perhaps because that echo got into a wrong place? Remove that echo. Why do you need it? Or place that echo at a place which would not interfere with your HTML code. e.g. perhaps wrap a div/span around where that echo is?
The INSERT returns no data or acknowledgement by itself. If you are using jQuery to display the inserted data, you will need to run another SELECT to get it again. You can also just return the primary key of the inserted row(assuming MySql for the link although the same thing works with PDO). See this
First of all I'm a rookie to Programming, I created a PHP page to update a value from my mysql(myadmin) database, but the value is not updating. I also tried to retrieve values from database it's working just fine but this UPDATE code is not working! I don't know why, please check out my code below.
$qs=mysql_query("update staff set review=$newrate where name=$rateuser");
$resu=mysql_query($qs);
All variables are double defined, assigned with proper values, checked and I tested variables using echo, table name is also checked, it's all fine, but I think the problem is with Update query, I searched internet for the syntax but it's not different than mine. Please help me out
How are $newrate and $rateuser set?
mysql_query("UPDATE staff SET review = '".mysql_real_escape_string($newrate)."' WHERE name = '".mysql_real_escape_string($rateuser) ."'");
http://php.net/manual/en/function.mysql-real-escape-string.php
Try:
$qs=mysql_query("update staff set review='$newrate' where name='$rateuser'");
Do not use second line.
You probably just need some " around your values $newrate and $rateuser
But if you did an echo, why not actually echo for us what the query-string becomes?
You need single quotes around string values on your query:
$qs=mysql_query("update staff set review='$newrate' where name='$rateuser'");
(assuming both variables are strings)
I am trying to input multiple pieces of data through a form and all the data will be separated by (,). I plan to use this data to find the corresponding id for further processing through an sql query.
Below is the code I use.
$key_code = explode(",", $keyword);
//$key_count = count($key_code);
$list = "'". implode("','", $key_code) ."'";
//$row_count = '';
$sql4= "SELECT key_id FROM keyword WHERE key_code IN (".$list.")";
if(!$result4 = mysql_query($sql4, $connect)) {
mysql_close($connect);
$error = true;
}else{
//$i = 0;
while($row = mysql_fetch_array($result4)) {
$keyword_id[] = $row['key_id'];
//$i++;
}
//return $keyword_id;
}
The problem i see is that keyword_id[0] is the only element that contains any data (the data is accurate). Even if I input multiple values through the aforementioned form.
I thought it might be an error in the sql but I echo'ed it and it looks like:
SELECT key_id FROM keyword WHERE key_code IN ('WED','WATER','WASTE')
The values in the brackets are exactly what I inputted.
I even tried to figure out how many rows are being returned by the query and it shows only 1. I assume something is wrong with my query but I cannot figure where.
Any help will be greatly appreciated.
Edit: Alright Solved the problem. Thanks to suggestions made I copied and pasted the $sql_query I had echo'ed on the website into mysql console; which resulted in only 1 row being retrieved. After taking a closer look I realized that there was a whitespace between ' and the second word. I believe the problem starts when I input the key_code as:
WED, WATER, WASTE
Instead inputting it as
WED,WATER,WASTE
fixes the problem. I think I should make it so that it works both ways though.
Anyway, thank you for the help.
I am pretty sure that the query is ok. How many rows do you get with just
SELECT key_id FROM keyword
I think that there is just one line that matches your WHERE.
Check the query directly in the database(with phpmyadmin, or in the mysql console), however this query seems to be working as you may assumed. If it returns only 1 row when you use it directly in the db, then maybe there is only one row in your table wich matches this query.