PHP - Insert Array Error
Similar TutorialsI need a help in the following : I have an admin page from where the admin attaches an gif image.This attached image should be shown to the user as a scrolling image. The code for the scrolling image is done through javascript. So my actual problem is getting the name of the attached gif image to the javascript array which is used for scrolling horizontally. I have an array $array_1 Code: [Select] ARRAY( [0] => info 1 [1] => info 2 [2] => info 3 ) and I have another array $array_2 Code: [Select] ARRAY( [0] =>thank you and welcome to what ever [1] =>the info you added * thanks for entering your info ) I want to loop though the first array and place the values into the [1] of the second array. But the tricky thing is I want it to insert were the * is. Code: [Select] foreach ($array_1 as $key => $val) { //not sure about this insert into ($array_2[1],after *, $val); } any help would be awesome. i am currently in the process of creating a registration page for my website, i have managed to make it work and it adds the user to my main table user_info, however i also have other tables that i want the system to insert the user into when they join, such as a log for their ip address, the problem is i thought it would be a simple cas of selecting the user info and then inserting the fields into the ip_address table, but i keep getting the error "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 'terminated')' at line 1" Anyway here is the code that i just added that created the error Code: [Select] $sql_id = ("SELECT * FROM user_info WHERE username = '$username'"); $result = mysql_query($sql_id); $sql_result = mysql_fetch_array($result); $sql = mysql_query("INSERT INTO ip_log (id, ip_log) VALUES ('$id', '$ipaddress'") or die (mysql_error()); And the code below is the code for the whole registration page. Thanks Code: [Select] <?php if (isset ($_POST['firstname'])){ $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $username = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['username']); // filter everything but letters and numbers $email = $_POST['email']; $password = $_POST['password']; $cpassword = $_POST['cpassword']; $paypal_email = $_POST['paypal_email']; $country = $_POST['country']; $kingdom_name = $_POST['kingdom_name']; $kingdom_motto = $_POST['kingdom_motto']; $referal = $_POST['referal']; $email = stripslashes($email); $password = stripslashes($password); $cpassword = stripslashes($cpassword); $email = strip_tags($email); $password = strip_tags($password); $cpassword = strip_tags($cpassword); // Connect to database include_once "connect_to_mysql.php"; $emailCHecker = mysql_real_escape_string($email); $emailCHecker = str_replace("`", "", $emailCHecker); // Database duplicate username check setup for use below in the error handling if else conditionals $sql_uname_check = mysql_query("SELECT username FROM user_info WHERE username='$username'"); $uname_check = mysql_num_rows($sql_uname_check); // Database duplicate e-mail check setup for use below in the error handling if else conditionals $sql_email_check = mysql_query("SELECT email FROM user_info WHERE email='$emailCHecker'"); $email_check = mysql_num_rows($sql_email_check); // Error handling for missing data if ((!$firstname) || (!$lastname) || (!$username) || (!$email) || (!$password) || (!$cpassword) || (!$paypal_email) || (!$kingdom_name) || (!$kingdom_motto)) { $errorMsg = 'ERROR: You did not submit the following required information:<br /><br />'; if(!$firstname){ $errorMsg .= ' * Firstname<br />'; } if(!$lastname){ $errorMsg .= ' * Lastname<br />'; } if(!$username){ $errorMsg .= ' * Username<br />'; } if(!$email){ $errorMsg .= ' * Email<br />'; } if(!$password){ $errorMsg .= ' * Password<br />'; } if(!$cpassword){ $errorMsg .= ' * Password Check<br />'; } if(!$paypal_email){ $errorMsg .= ' * Paypal Email<br />'; } if(!$kingdom_name){ $errorMsg .= ' * Kingdom Name<br />'; } if(!$kingdom_motto){ $errorMsg .= ' * Kingdom Motto<br />'; } } else if ($password != $cpassword) { $errorMsg = 'ERROR: Your Password fields below do not match<br />'; } else if (strlen($username) < 4) { $errorMsg = "<u>ERROR:</u><br />Your User Name is too short. 4 - 20 characters please.<br />"; } else if (strlen($username) > 20) { $errorMsg = "<u>ERROR:</u><br />Your User Name is too long. 4 - 20 characters please.<br />"; } else if ($uname_check > 0){ $errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside of our system. Please try another.<br />"; } else if ($email_check > 0){ $errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside of our system. Please use another.<br />"; } else { // Error handling is ended, process the data and add member to database //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $email = mysql_real_escape_string($email); $password = mysql_real_escape_string($password); // Add MD5 Hash to the password variable $db_password = md5($password); // GET USER IP ADDRESS $ipaddress = getenv('REMOTE_ADDR'); // Add user info into the database table for the main site table $sql = mysql_query("INSERT INTO user_info (firstname, lastname, username, email, password, country, sign_up_date) VALUES('$firstname','$lastname','$username','$email','$password', '$country', now())") or die (mysql_error()); $id = mysql_insert_id(); // Create directory(folder) to hold each user's files(pics, MP3s, etc.) mkdir("members/$id", 0755); //////////////////////////////////////////////////////////////////////// ///////////////BUILDING THE USER PROFILES/////////////////////////////// $sql_id = ("SELECT * FROM user_info WHERE username = '$username'"); $result = mysql_query($sql_id); $sql_result = mysql_fetch_array($result); $sql = mysql_query("INSERT INTO ip_log (id, ip_log) VALUES ('$id', '$ipaddress'") or die (mysql_error()); include_once 'registration_success.php'; exit(); } // Close else after duplication checks } else { // if the form is not posted with variables, place default empty variables so no warnings or errors show $errorMsg = ""; $firstname = ""; $lastname = ""; $username = ""; $email = ""; $password= ""; $cpassword = ""; $paypal_email = ""; $kingdom_name = ""; $kingdom_motto = ""; $referal = ""; } ?> Can anyone see something wrong with this? It's driving me crazy and throwing up an error Quote 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 ''user_name', 'user_email', 'user_credit', 'fname', 'lname', 'hous' at line 3 Code: [Select] $sql_insert = ("INSERT INTO users ( 'user_name', 'user_email', 'user_credit', 'fname', 'lname', 'house_num', 'addr', 'addr2', 'county', 'postcode', 'pwd', 'dobDay', 'dobMonth', 'dobYear', 'tel', 'date', 'users_ip', 'avatar' ) VALUES ( '".$user_name."', '".$usr_email."', '0.00', '".$fname."', '".$lname."', '".$house_num."', '".$addr."', '".$addr2."', '".$county."', '".$postcode."', '".$sha1pass."', '".$dobDay."', '".$dobMonth."', '".$dobYear."', '".$tel."', '".$date."', '".$user_ip."', '/images/default/avatar.png' )"); echo $sql_insert; mysql_query($sql_insert,$link) or die(mysql_error()); I can't see what i wrong with it, i removed all the blank entries (ones with the value 'null',) but it didn't make any difference Hi people I am new to php and I need your help.
I am usig libchart and want to make the bars colored based on value:
Original code:
$this->setBarColor(array( new Color(42, 71, 1), )); I want to make something like this: $this->setBarColor(array( if ( ( condition1 )) new Color(42, 171, 1), if ( ( condition2 )) new Color(42, 71, 1), if ( ( condition3 )) new Color(42, 71, 211), )); I do not know php syntax yet, so how can I do this? Thank you, Alex Hi, I am working on a script that will add 3 sets of information to a MySQL table. I had a script that had the fields of username password and password2. It worked just fine when password2 was used for verification to make sure you typed it right. But now I want to take password and turn it into a new data field that is submitted into the new row with the other information. The problem is it wont add the row. The script says it worked just fine but i check the db and no new row. here it is: <?php // Connects to your Database mysql_connect("//", "//", "//") or die(mysql_error()); mysql_select_db("//") or die(mysql_error()); //This code runs if the form has been submitted if (isset($_POST['submit'])) { //This makes sure they did not leave any fields blank if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) { die('You did not complete all of the required fields'); } // checks if the username is in use if (!get_magic_quotes_gpc()) { $_POST['username'] = addslashes($_POST['username']); } $usercheck = $_POST['username']; $check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); //if the name exists it gives an error if ($check2 != 0) { die('Sorry, the username '.$_POST['username'].' is already in use.'); } // this makes sure both passwords entered match if ($_POST['pass'] != $_POST['pass']) { die('Your passwords did not match. '); } // here we encrypt the password and add slashes if needed $_POST['pass'] = md5($_POST['pass']); if (!get_magic_quotes_gpc()) { $_POST['pass'] = addslashes($_POST['pass']); $_POST['username'] = addslashes($_POST['username']); $_POST['pass2'] = addslashes($_POST['pass2']); } // now we insert it into the database $insert = "INSERT INTO users (username, password, Human-Readable) VALUES ('".$_POST['username']."', '".$_POST['pass']."', '".$_POST['pass2']."')"; $add_member = mysql_query($insert); ?> <h1>Registered</h1> <p>Thank you, you have registered - you may now login</a>.</p> <?php } else { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table border="0"> <tr><td>Username:</td><td> <input type="text" name="username" maxlength="60"> </td></tr> <tr><td>Password:</td><td> <input type="password" name="pass" maxlength="10"> </td></tr> <tr><td>HR:</td><td> <input type="password" name="pass2" maxlength="10"> </td></tr> <tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr> </table> </form> <?php } ?> And incase you want to see it here is the original: <?php // Connects to your Database mysql_connect("//", "//", "//") or die(mysql_error()); mysql_select_db("//") or die(mysql_error()); //This code runs if the form has been submitted if (isset($_POST['submit'])) { //This makes sure they did not leave any fields blank if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) { die('You did not complete all of the required fields'); } // checks if the username is in use if (!get_magic_quotes_gpc()) { $_POST['username'] = addslashes($_POST['username']); } $usercheck = $_POST['username']; $check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); //if the name exists it gives an error if ($check2 != 0) { die('Sorry, the username '.$_POST['username'].' is already in use.'); } // this makes sure both passwords entered match if ($_POST['pass'] != $_POST['pass2']) { die('Your passwords did not match. '); } // here we encrypt the password and add slashes if needed $_POST['pass'] = md5($_POST['pass']); if (!get_magic_quotes_gpc()) { $_POST['pass'] = addslashes($_POST['pass']); $_POST['username'] = addslashes($_POST['username']); } // now we insert it into the database $insert = "INSERT INTO users (username, password) VALUES ('".$_POST['username']."', '".$_POST['pass']."')"; $add_member = mysql_query($insert); ?> <h1>Registered</h1> <p>Thank you, you have registered - you may now login</a>.</p> <?php } else { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table border="0"> <tr><td>Username:</td><td> <input type="text" name="username" maxlength="60"> </td></tr> <tr><td>Password:</td><td> <input type="password" name="pass" maxlength="10"> </td></tr> <tr><td>Confirm Password:</td><td> <input type="password" name="pass2" maxlength="10"> </td></tr> <tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr> </table> </form> <?php } ?> Many Thanks in advanced
<?php
<!DOCTYPE html>
I am having an error with this code You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''name','email','password','profile') SET ('Sasural','kill@1234.com','kill','ANDK' at line 1 I am stuck with this for last 5hrs Hi. I think you all know me by now so I'll cut to the chase. Code: [Select] <?php $host="edited"; $username="edited"; $password="edited"; $db_name="edited"; $tbl_name="topic"; // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $name=$_POST['name']; $detail=$_POST['details']; $sql="INSERT INTO $tbl_name(topic, detail, datetime)VALUES('$name', '$detail', NOW())"; $result=mysql_query($sql); if($result){ header("location:site.html");} else{ echo("I have failed you master.");} ?> Displayed error: "I have failed you master." Anyone know a possible cause? Thanks. Bye. I have a Form on registration.html through which i trying to get data in mysql through the below php script but there is and mysql syntax error please help me with the below code. Code: [Select] <?php $conn = mysql_connect("localhost", "onlinewe_meghraj", "password123") or die(mysql_error()); $db = mysql_select_db("onlinewe_college") or die(mysql_error()); $name1 = $_POST['name1']; $name2 = $_POST['name2']; $year = $_POST['year']; $department = $_POST['deparment']; $group = $_POST['group']; $in_name = $_POST['in_name']; $in_address = $_POST['in_address']; $phone = $_POST['phone']; $email = $_POST['email']; $mobile1 = $_POST['mobile1']; $mobile12 = $_POST['mobile2']; $comment = $_POST['comment']; $result=mysql_query("INSERT INTO register (name1, name2, year, department, group, in_name, in_address, phone, email, mobile1, mobile2, date, comment) VALUES ('$name1', '$name2', '$year', '$department', '$group', '$in_name', '$in_address', '$phone', '$email', '$mobile1', '$mobile2', '".date("Y-m-d h:i:s")."', '$comment')") or die("Insert Error: ".mysql_error()); echo "REGISTRATION DONE"; ?> Please reply. Thank you. Having a problem with that "insert ignore into". If it is a new record, it works, when it is a excisting record that needs to be updated, it is giving me this error: Cannot execute query: INSERT IGNORE INTO (gedcom,kennel) VALUES ("World","Avongara") What or where is it going wrong? This message is for me not enough to find the problem. $kennels = array(); if( !in_array( $kennel, $kennels ) ) array_push( $kennels, $kennel ); foreach( $kennels as $kennel ) { $query = "INSERT IGNORE INTO $kennels_table (gedcom,kennel) VALUES (\"$tree\",\"$kennel\")"; $result = @mysql_query( $query ) or die ("$admtext[cannotexecutequery]: $query"); } Thanks for helping Hey guys, this is my first post here(not going to be the last one, Im sure), im trying to insert in mysql from session array, i don't know where is my error, I leave the code below, if someone can help me please . Can you guys tell me where is the error please...
$sql2= ('CREATE TABLE `'.$pub_unik.'` (ID SERIAL,ID_Use CHAR(30),Comment TEXT,Like INT,Score CHAR(30))');I can't find... Thank you guys If I have an array like this, what makes the most sense for inserting a key value pair after a given key? This is how I came up with to do it, after looking at the manual I'm not sure if there's a function I'm missing, because this seems way too complicated. Code: [Select] <?php function addKV($arr, $keyToFind, $addKey, $addValue){ $keys = array_keys($arr); $spot = array_search($keyToFind, $keys); $chunks = array_chunk($arr, ($spot+1)); array_unshift($chunks[1], array($addKey=>$addValue)); $arr = call_User_Func_Array('array_Merge', $chunks); return $arr; } $myArr = array('id'=>1, 'name'=>'Name', 'created'=>time(), 'modified'=>time()); $myArr = addKV($myArr, 'name', 'foo', 'bar'); ?>$myArr now looks like Code: [Select] array('id'=>1, name=>'Name', 'foo'=>'bar', 'created'=>time(), 'modified'=>time()); Hi - I have an HTML form which is dynamically produced using CodeIgniter / PHP and presents products a customer has ordered. When the customer has finished changing his quantities they must now submit the form. My problem is that my form will contain many of the same named fields for example, 'Product ID', or 'Product Name' or 'quantity'. So now when it gets processed a typical post array like product id could look like this: Code: [Select] Array ( [0] => 28 [1] => 24 [2] => 101 [3] => 23 [4] => 17 ) Of course I get an error: Quote Severity: Notice Message: Array to string conversion Filename: mysql/mysql_driver.php I really need some advice on how I should pre-process the various POST arrays so I can get them into my DB. A very grateful student of PHP ! Thanks I'm trying to insert array of posts categories into MySQL. I'm getting category IDs from drop down list.. ERR: Notice: Notice: Array to string conversion in C:\laragon\*********\includes\functions.php on line 477
Array Array ( [0] => Array ( [0] => 4 [1] => 5 ) )
PHP try { $sql = "INSERT INTO posts_category_ids (post_id, category_id) VALUES"; $insertQuery = array(); $insertData = array(); foreach ($filter_category as $row) { $insertQuery[] = '(?, ?)'; $insertData[] = $id; $insertData[] = $row; } if (!empty($insertQuery)) { $stmt = $this->conn->prepare($sql); $stmt->execute($insertData); //477 } }catch (Exception $e){ echo $e->getMessage(); } }
Hi, I am creating a new menu (food) in my system. This consists of a menu, menu_items and menu_connection table. I can insert the menu name just fine and return its id just fine. When inserting the menu items, i need to get each of the menu_item_ids to use in the query that inputs the menu_connection. This is what i have so far: if ($_SERVER['REQUEST_METHOD']=="POST") { ///////////////////// //menu name insert // ///////////////////// $mname = mysqli_real_escape_string($conn, $_POST['newMenuName']); $stmt=$conn->prepare(' INSERT IGNORE INTO ssm_menu (menu_name) VALUES (?); '); $stmt->bind_param('s',$mname); $stmt->execute(); $menuInsId = $stmt->insert_id; echo $menuInsId; $stmt->close(); ///////////////////// //menu item insert // ///////////////////// $mitname = $_POST['newMenuItem']; $stmt=$conn->prepare(' INSERT IGNORE INTO ssm_menu_items (menu_item_name) VALUES (?); '); foreach ($_POST['newMenuItem'] as $k => $nmItem) { $mitname = mysqli_real_escape_string($conn, $nmItem); $stmt->bind_param('s',$mitname); $stmt->execute(); $menuItmInsId = $stmt->insert_id; echo $menuItmInsId; } $stmt->close(); /////////////////////////// //menu connection insert // /////////////////////////// $stmt=$conn->prepare(' INSERT IGNORE INTO ssm_menu_connection (menu_id, menu_item_id) VALUES (?,?) '); foreach ($_POST['newMenuItem'] as $k => $nmItem) { $stmt->bind_param('ii',$menuInsId, $menuItmInsId); $stmt->execute(); $connectionInserId = $stmt->insert_id; echo $connectionInserId; } $stmt->close(); } Currently it is inserting each of the items in the connection table with the same id - i understand why but i dont know how to collect up all of the ids to use later HELP!!! PLEASE!!! Here's what's happening: I have a string much like this one: Code: [Select] $str = "84,390961CPK_100,'Pink Coloured Cord',15,'390961CPK_100',1"; I need to insert apostrophes around the first instance of 390961CPK_100 so it reads '390961CPK_100' and not simply 390961CPK_100. To do this I have made the string into an array as follows: Code: [Select] $str_array = str_split($str); Now I need to insert quotes into the array of characters, then convert the whole mess into back into a string. I can't go by hard coded index numbers, because all of the text in this string is variable in length, so I have to go by the first and second positions of the commas. I have no idea how to do this, and it needs to be done yesterday! Mucho Gracias to anyone who can help me with this. I looked through all the array functions in the manual, but didn't see what I needed (might have just missed it) Is there a way to stick a value into the middle of an array, without writing over the current value? I would need it to just push the rest back 1. Hope that explains what Im looking for! Thanks in advance Hi again , I am stuck with simplest problem of array .. i searched all forums and couldn't find similar problem.. Iam collecting array post data "data m_name[][email]" from a form, but I don't know how to get 2 array variables at the same time, here's what I tried : <form action="" method="post" name="myForm"> <div> <input type="text" name="m_name[]" /> <input type="text" name="m_name[][email]" /> </div> <div> <input type="text" name="m_name[]" /> <input type="text" name="m_name[][email]" /> </div> <div> <input type="text" name="m_name[]" /> <input type="text" name="m_name[][email]" /> </div> <input type="submit" name="button" id="button" value="Submit" /> </form> other page where I want to receive above array <?php if($_POST) { print_r($_POST['m_name']); // Output is : Array ( [0] => someone [1] => Array ( [email] => 3 ) [2] => someone two [3] => Array ( [email] => 4 ) [4] => someone three [5] => Array ( [email] => 5 ) ) while (list ($key,$val) = @each ( $_POST['m_name'])) { echo $val.val2 ???"<br/>"; // here I want to get both value $_POST['m_name'] and $_POST['m_name'] ['email'] since I am putting them in db << } } ?> I want to get both values of $_POST['m_name'], I don't know how to do it or is there another way to do it? thankx in advance!! phpfreaks have been very helpful .. |