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
Related
I want to load data from a SQLite database in PHP using Atlas.Orm.
When I run the following snippet, I get a set of 1773 results, but each result is the same!
$atlas = Atlas::new('sqlite:[Path To Database]');
$result = $atlas->select(Stop::class)->fetchRecords();
Can anyone tell me whats wrong here?
After several hours of desperation, I found the issue by myself. The ORM needs the PRIMARY_KEY constant in the corresponding *Table class to be set, otherwise fetching records will fail like this.
I have come across a strange problem, which im trying to solve for quite some time now but can´t find any solution to this.
I am generating some lines with information which each of includes one checkbox. I have the following code in PHP which checks if a certrain entry exists, if so the checkbox is checked.
$sql = "SELECT COUNT(*) anz FROM jubilaeum WHERE jahr='".$Jahr."' AND mon='".$num."' AND AdrNr='".$RS1_row["AdrNr"]."' AND type='1'";
$rs_erledigt = $db->prepare($sql);
$rs_erledigt->execute();
$row = $rs_erledigt->fetch();
$anz = $row["anz"];
The Code generates me the following SQL Query:
SELECT COUNT(*) anz FROM jubilaeum WHERE jahr='2019' AND mon='5' AND AdrNr='14061' AND type='1'
PHPMyAdmin Query & Result
Now i am using a basic IF to check if there are any records found so i can check a checkbox
<input type="checkbox" name="mychk" id="mychk" value="somevalue" <?php if($anz>0) echo "checked"; ?> />
All checkboxes which have a proper entry in my DB are checked, except the very first one generated, i can swap the boxes around at free will, the first one never gets checked.
I tried to use the $row["Anz"] directly in my IF, didn´t fix the problem.
I think that PHP doesnt interpret the returned value of my query correctly, but i am clueless about how to fix this.
Did someone encounter similar problems and can help me with this?
Im new to posting in here, so please tell me if you need some more information.
Thanks in advance!
Edit: I just tried to change the Query from COUNT(*) to if(COUNT(*)>0,'ja','nein') while also changing the if to if($anz=="ja")
the value of $anz still remains empty.
I found the solution. My issue was a second fetch on my Query. After removing it, everything works fine.
I have an external database that I am trying to access from within a Drupal page, I have successfully queried the database and output data to the page using fetchAssoc(), however this only returns the first row in the database. I would like to return all rows into an array for processing, so I'm attempting to use fetchAllAssoc(), this however results in an exception. The database has the following SQL fields:
id, model, manufacturer, url, date_modified
My test code is as follows:
<?php
db_set_active('product_db');
$query = db_select('product', 'p')->fields('p');
$sqlresults = $query->execute()->fetchAllAssoc('id');
foreach($sqlresults as $sqlresult)
{
printf($sqlresult);
}
db_set_active();
?>
I'm thinking that it is the key field 'id' that I am specifying with fetchAllAssoc() that is the problem, as fetchAssoc() prints values correctly. All documentation I have found seems to say that you pass a database field as the key but I have also passed a numeric value with no success.
Many thanks in advance for any advice, I'm sure I'm just missing something stupid.
I think it should work in this way, but within the foreach you want to print the $sqlresult variable as a string, but it is an object (it causes the error).
printf function needs a string as the first parameter, see:
http://php.net/manual/en/function.printf.php
Use for instance var_dump instead:
var_dump($sqlresult);
I am trying to extend the Zend library in order to get queries like this one:
SELECT * FROM item i **force index(ix)** ORDER BY date LIMIT 100;
I am working arround with this Zend forum issue solution: http://framework.zend.com/issues/browse/ZF-7570
However I have a problem. Look at the code below.
$select->forceIndex('index');
echo $select->assemble();
// I get the right SELECT query with the force index hint
$this->fecthAll($select);
// The server execute a wrong SELECT query without the force index hint
Some help?
I am not pretty sure why, but if I remove the echo clause, fetchAll executes the right query...
So that's the answer, remove echo
In my opinion, the patch you are using is broken. If you check the source, it unsets the FORCE INDEX parts when creating the query the first time.
If you check it, $select->assemble() == $select->assemble() should give false.
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());