PHP - Moved: Not Sure How Can Do This? Insert Into()
This topic has been moved to MySQL Help.
http://www.phpfreaks.com/forums/index.php?topic=309473.0 Similar TutorialsThis topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=350408.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=315743.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=357712.0 This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=352154.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=319161.0 This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=308768.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=354933.0 This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=344267.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=342913.0 This topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=346503.0 Amazingly, PHP Coding Help != JavaScript Help. This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=333336.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=315503.0 I'm missing something here. I have a form, and when the submit is pressed, the relevant post data inserts into table one, then I want the last insert id to insert along with other form data into a second table. The first table's still inserting fine, but I can't get that second one to do anything. It leapfrogs over the query and doesn't give an error. EDIT: I forgot to add an error: I get: 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 'usage, why VALUES ('14', '', '123', '','1234', '', '')' at line 1 query:INSERT INTO tbl_donar (donar_fname, donar_name, donar_address, donar_address2, donar_city, donar_state, donar_zip, donar_email, donar_phone, donar_fax, donar_company) VALUES ('test 14', 'asdfa', 'asdf', 'adf','asdf', '', '', '', '123', '', '') Code: [Select] if (empty($errors)) { require_once ('dbconnectionfile.php'); $query = "INSERT INTO tbl_donar (donar_fname, donar_name, donar_address, donar_address2, donar_city, donar_state, donar_zip, donar_email, donar_phone, donar_fax, donar_company) VALUES ('$description12', '$sn', '$description4', '$cne','$description5', '$description6', '$description7', '$description8', '$description9', '$description10', '$description11')"; $result = @mysql_query ($query); if ($result) { $who_donated=mysql_insert_id(); $query2 = "INSERT INTO tbl_donation (donor_id, donor_expyear, donor_cvv, donor_cardtype, donor_authorization, amount, usage, why) VALUES ('$who_donated', '$donate2', '$donate3', '$donate4','$donate5', '$donate6', '$donate7')"; $result2 = @mysql_query ($query2); if ($result2) {echo "Info was added to both tables! yay!";} echo "table one filled. Table two was not."; echo $who_donated; //header ("Location: http://www.twigzy.com/add_plant.php?var1=$plant_id"); exit(); } else { echo 'system error. No donation added'; Can anyone tell me why this is not INSERTing? My array data is coming out just fine.. I've tried everything I can think of and cannot get anything to insert.. Ahhhh! <?php $query = "SELECT RegionID, City FROM geo_cities WHERE RegionID='135'"; $results = mysqli_query($cxn, $query); $row_cnt = mysqli_num_rows($results); echo $row_cnt . " Total Records in Query.<br /><br />"; if (mysqli_num_rows($results)) { while ($row = mysqli_fetch_array($results)) { $insert_city_query = "INSERT INTO all_illinois SET state_id=$row[RegionID], city_name=$row[City] WHERE id = null" or mysqli_error(); $insert = mysqli_query($cxn, $insert_city_query); if (!$insert) { echo "INSERT is NOT working!"; exit(); } echo $row['City'] . "<br />"; echo "<pre>"; echo print_r($row); echo "</pre>"; } //while ($rows = mysqli_fetch_array($results)) } //if (mysqli_num_rows($results)) else { echo "No results to get!"; } ?> Here is my all_illinois INSERT table structu CREATE TABLE IF NOT EXISTS `all_illinois` ( `state_id` varchar(255) NOT NULL, `city_name` varchar(255) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; Here is my source table geo_cities structu CREATE TABLE IF NOT EXISTS `1` ( `CityId` varchar(255) NOT NULL, `CountryID` varchar(255) NOT NULL, `RegionID` varchar(255) NOT NULL, `City` varchar(255) NOT NULL, `Latitude` varchar(255) NOT NULL, `Longitude` varchar(255) NOT NULL, `TimeZone` varchar(255) NOT NULL, `DmaId` varchar(255) NOT NULL, `Code` varchar(255) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; Hello, I'm having a bit of a problem here, all help to this issues would be much appreciated I am trying to use text boxes to insert numbers into the database based on what is inputed. If I have a string, like this for example: $variable = 09385493; And I want to insert it into the database like this: mysql_query("INSERT INTO integers(number) VALUES ('$variable')"); When checking the integers table in my database, looking at the number field, the $variable that was inserted is outputted as 9385493 Notice the number zero was taken out of the front of the number. If the number is double 0's (009385493), both of those zero's would disappear, too. Thanks Okay, I'm hoping one of you can help me. I have a mysql database that I have configured through phpmyadmin. I have an android app that simply makes and sends a mysql query I can get it to successfully return values when using Select statements but when I use INSERT INTO, it returns " Error Query is invalid" BUT BUT BUT, when I use the same string and enter it through the sql tab in myphpadmin it works fine ! So here is the string ( the semicolons at the end of each field name are so I can use something common to split the string up when the data arrives back on the phone) Code: [Select] randomkey||||||INSERT INTO table4 (`geolat;` , `geolong;` , `mode;` , `destgeolat;` , `destgeolong;` , `cellphone;` , `email;` , `carrego;` , `colour;` , `rating;` , `comment;`) VALUES (0.0,0.0,'driver' ,-43.54779,172.62472, , '' ,'' , 'text' , 'ratingleftblank' , 'commentblank' ) the index4.php script is as follows Code: [Select] ?php /* * Written By: * James */ /************************************CONFIG****************************************/ //DATABSE DETAILS// $DB_ADDRESS="mysql1.openhost.net.nz"; $DB_USER="bling44"; $DB_PASS="sadlyinept"; $DB_NAME="bling44"; //SETTINGS// //This code is something you set in the APP so random people cant use it. $SQLKEY="randomkey"; /************************************CONFIG****************************************/ //these are just in case setting headers forcing it to always expire and the content type to JSON header('Cache-Control: no-cache, must-revalidate'); header('Content-type: application/json'); if(isset($_POST['tag'])){ //checks ifthe tag post is there $tag=$_POST['tag']; $data=explode("||||||",$tag); //split the SQL statement from the SQLKEY if($data[0]==$SQLKEY){ ///validate the SQL key $query=$data[1]; $link = mysql_connect($DB_ADDRESS,$DB_USER,$DB_PASS); //connect ot the MYSQL database mysql_select_db($DB_NAME,$link); //connect to the right DB if($link){ $result=mysql_query($query); //runs the posted query (NO PROTECTION FROM INJECTION HERE) if($result){ if (strlen(stristr($query,"SELECT"))>0) { //tests if its a select statemnet $outputdata=array(); while ($row = mysql_fetch_assoc($result)){ $outputdata[]=$row; //formats the result set to a valid array } echo json_encode(array("VALUE",$tag,array_merge($outputdata))); //sends out a JSON result with merged output data } else { echo json_encode(array("VALUE",$tag,array_merge(array(array("AFFECTED_ROWS ".mysql_affected_rows($link)))))); //if the query is anything but a SELECT it will return the array event count } } else echo json_encode(array("VALUE",$tag,array_merge(array(array("ERROR QUERY IS INVALID"))))); //errors if the query is bad mysql_close($link); //close the DB } else echo json_encode(array("VALUE",$tag,array_merge(array(array("ERROR Database Connection Failed"))))); //reports a DB connection failure } else { echo json_encode(array("VALUE",$tag,array_merge(array(array("ERROR BAD CODE SUPPLIED"))))); //reports if the code is bad } } ?> So to reiterate. I can search the DB but can't INSERT INTO, unless I go through the myphpadmin interface. Any ideas are very much appreciated Would someone be able to tell me why I can't run a successful mysql_query($sql) with $sql="INSERT INTO myDB.Titles (Title, Year, cID) values('". $title . "', '" . $year . "', '" . $cID . "')"; $sql echoes out to "INSERT INTO myDB.Titles (Title, Year, cID) values('Test', '2000', '2')" It looks right but I'm probably off on the quotes somewhere. Thanks in advance. I'm trying to use PDO and get used to doing things this way. I've been away from php/mysql for a few years, so, I'm crusty. I'm not getting any error messages back on this code, but the insert just doesn't happen. My first guess is that I'm doing something wrong with the datetime now() function. But, I may not have the PDO code right. I tried the script the old fashion way with mysql_query() and that worked. So, it has to be something in this code. I believe my server is set up to do PDO as it shows: PDO PDO support enabled PDO drivers mysql, sqlite pdo_mysql PDO Driver for MySQL, client library version 5.0.45 My php version is 5.2.14 and Mysql is 5.0.45. Any help would be appreciated. Code: [Select] $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $sql=$DBH->prepare("INSERT INTO assets(asset_name,date_added,short_desc) VALUES (:asset_name,NOW(),:short_desc)"); $sql->bindParam(':asset_name',$asset_name); $sql->bindParam(':short_desc',$short_desc); $name=$_POST["input1"]; $short_desc=$_POST["input2"]; $DBH->exec(); echo $name; echo "\nPDO::errorInfo():\n"; print_r($DBH->errorInfo()); } catch(PDOException $e) { echo "Syntax Error: ".$e->getMessage(); } I get the following message: Quote name4Query failed: Unknown column 'late' in 'field list' with this code. What does it mean? Code: [Select] <?php $apt=$_POST['search_term']; $stat = mysql_connect("localhost","root",""); $stat = mysql_select_db("prerentdb"); $query = "SELECT name FROM payments WHERE late = 'L'"; $stat = @mysql_fetch_assoc(mysql_query($query)); echo $stat["name"]; $name=$_POST['name']; $apt=$_POST['apt']; $amtpaid=$_POST['amtpaid']; $rentdue=$_POST['rentdue']; $prevbal=$_POST['prevbal']; $hudpay=$_POST['hudpay']; $tentpay=$_POST['tentpay']; $datepaid=$_POST['datepaid']; $late=$_POST['late']; $comments=$_POST['comments']; $paidsum=$_POST['paidsum']; $query = " INSERT INTO payhist (name,apt,amtpaid,rentdue,prevbal, hudpay,tentpay,datepaid,late,comments,paidsum) VALUES('$name','$apt','$amtpaid','$rentdue','$prevbal', '$hudpay','$tentpay','$datepaid','$late','$comments','$paidsum')"; $stat = mysql_query($query) or die('Query failed: ' . mysql_error()); mysql_close(); echo "data inserted<br /><br />"; ?> Hey, who can help? Example: I want to insert a row with a input and a select option. I am inserting now with select option, but i want to insert with a input too, because if i need to insert a link instead of choosing from select option. --------- (puts a link)Link: ...link... (chooses nothing)Select: Choose one... Submit --------- I need something like if LINK is filled insert it. Nothing happens to SELECT, but they have to have the same row names. Hope its clear enough. Cheers |