PHP - Problem With Sql Entry...
Well this sounds weird, but it happens. I have an itemshop script, and I wanna code a system that automatically delete the database entry once the variable item amount falls to 0. However, this doesnt happen when I check phpmyadmin, and the item remains in the database even after the amount becomes 0. In fact, the amount can become negative at times, which annoys me.
The code of deleting sql entry looks like this: if($continue == "yes"){ $query = "SELECT * FROM ".$prefix."user_inventory WHERE item_owner = '$loggedinname' AND item_name = '$item_name'"; $result = mysql_query($query); $num = mysql_numrows($result); $item_data = mysql_fetch_array($result); $item_amount = $item_data['item_amount']; $newquantity = $item_amount - $quantity; $query = "UPDATE ".$prefix."user_inventory SET item_amount = '$newquantity' WHERE item_owner = '$loggedinname' AND item_name = '$item_name'"; $result = mysql_query($query); if($newquantity == "0"){ $query = "DELETE FROM ".$prefix."user_inventory WHERE item_owner = '$loggedinname' AND item_name = '$item_name'"; $result = mysql_query($query); } What part of the codes should I edit to fix this issue? Thanks. Similar TutorialsAlright, wasn't quite sure how to summarize this in the title, but I want to: Check if a user status is "active" or not based on the UserName input. I have a table witch holds: Code: [Select] VarChar Username Var CharPassWord int Active Ted TedsPW 1 something like the above(assuming it formatted correctly. In my php script I will want to input a variable for Username to check for: inputUN in this example would be "Ted". $UserNameToCheck = $_GET['inputUN']; Then I want to check for that UserName in the database, if it exists, I want pull the value for the "Active" field for just that UserName and echo it. Thanks for any help. I am using php to upload a file to my server, and at the same time inserting the files name and url into my mysql database.
$sql = "UPDATE uploads SET name = '$name', url='$target_path'"; $statement = $dbh->prepare($sql); $statement->execute();This is working, however, when I upload a new file, rather than making a new entry in my database, it just overwrites the first one. I'm quite new at mysql so was wondering how I would make it add new entrys instead of overwriting the current one? Hello Everyone, I have built a simple registration form shown below and Iam trying to get the users to have their passwords encrypted and then entered into my database. I am attempting to use md5 encryption. I have also attached the database connection script. My goal is when I check my database, I want to see the following: ( id, name, username, encrypted password ) The issue I have is that the form does not process completely. All I get this error (Error: Unknown column 'd8578edf8458ce06fbc5bb76a58c5ca4' in 'field list' ). Could some tell me or show me " What is it that needs to be corrected either in my Code or SQL insert and /or my Variables" to make this work correctly. I know that its probably a very, very simple fix... Im just stuck at this point. I really appreciate your help. thanks, mrjap1 Code: [Select] <?php error_reporting(0); if($_POST['submit']) { //Begining of full IF Statment $name = $_POST['name']; $username = $_POST['username']; $password = $_POST['password']; $confirm_password = $_POST['confirm_password']; // Encrypt Pasword $enc_password = md5($password); //$enc_password2 = md5($confirm_password); // Confirm All feild were filled out when submit button was pressed if($name && $username && $password && $confirm_password) { // Confirm that the NAME that you used is NOT greater than 30 characters if(strlen($name)>24) { echo "<h2><center>YOUR NAME IS TOO LONG!!!!</center></h2><br>"; } // Confirm that the USERNAME that you used is NOT greater than 10 characters if(strlen($username)>10) { echo "<h2><center>YOUR USERNAME IS TOO LONG!!!!</center></h2><br>"; } else { // Confirm that the PASSWORD that you used MATCH & Between 6 and 15 characters if(strlen($password)>10 || strlen($password)<6) { echo "<h2><center>YOUR PASSWORD MUST BE BETWEEN 6 and 15 CHARACTERS!!!!</center></h2><br>"; } if($password == $confirm_password) { // Database Connection required require "db_conncect.php"; // We Now connect to the Dabase and insert the Form input details //------- ### ENTERING ALL INFORMATION INTO THE DATABASE BELOW ### --------// // 1. Create a database connection $con = mysql_connect("localhost","root",""); // <-- THIS IS WHERE YOU " CAN CHANGE " THE USERNAME IS "root", PASSWORD IS "" ONLY. if (!$con) { die('Database connection failed could not connect: ' . mysql_error()); } // 2. Select a database to use $db_select = mysql_select_db("registernow_2012",$con); // <-- THE "registernow_2012" IS THE NAME OF THE DATABASE. if (!$db_select) { die('Database selection failed could not connect: ' . mysql_error()); } mysql_select_db("registernow_2012", $con); // <-- THE "registernow_2012" IS THE NAME OF THE DATABASE TO BE CONNECTED. // <-- THE `registernow_2012` IS THE NAME OF THE DATABASE TO BE CONNECTED.... `visitors` IS THE TABLE WITH ALL THE FIELDS WITHI IN THE DATABASE. $sql="INSERT INTO `registernow_2012`.`users` ( `id` , `name` , `username` , `$enc_password` , `confirm_password` ) VALUES ( NULL , '$_POST[name]', '$_POST[username]', '[$enc_password]', '$_POST[confirm_password]' )"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } // 3. Close Connection mysql_close($con); header("Location: index.php"); // <-- THIS IS WHERE YOU CAN CHANGE THE "Location: Thank you / Index page" of the THANK YOU PAGE. } else { echo "<h2><center>PASSWORDS MUST MATCH!!!!!</center></h2><br>"; } } //echo "<h2><center>WORKING!!!!</center></h2>"; } else echo "<h2><center>ALL FEILDS MUST BE COMPLETED</center></h2>"; } //Ending of full IF Statment ?> <!DOCTYPE html> <html lang='en'> <head> <title>THE FORM MY WAY NOW</title> </head> <div id='centerstage'> <form name="myform" action="workingitoutproperly.php" method="POST"> <p> <label>Name</label><br> <input type='text' name='name' value=''><br> <label>UserName</label><br> <input type='text' name='username' value=''><br> <label>Password</label><br> <input type='password' name='password' value=''><br> <label>Re-Enter Password</label><br> <input type='password' name='confirm_password' value=''><br> <br> <input type='submit' name='submit' value='REGISTER NOW!!'> </p> </form> </div> </html> $insertCount=0; foreach($results[1] as $curName) { if($insert){$insertCount++;} echo <<< END $curName<BR> END; } Right now the results would show up as... Bill Fred Jessica James John How do you make them show up like... 1 Bill 2 Fred 3 Jessica 4 James 5 John i'm trying to get the username of the last person to enter something into a database table,and display it an html table. the problem is, the query is failing, and i get the error "Table 'forum.posts' doesn't exist". if in the error message "forum" means the name of the database, and "posts" the name of the table, there is something wrong, as they both exist. the code i'm using is below $list = "SELECT * FROM section_main ORDER BY section_title"; $result = mysql_query($list) or die ("Query failed"); $numofrows = mysql_num_rows($result); for($j = 0; $j < $numofrows; $j++) { $row = mysql_fetch_array($result); echo "<tr><th>". $row['section_title']."</th></tr>"; $query2 = "SELECT * FROM section_sub WHERE section_id = '".$row['section_id']."' ORDER BY section_sub_title"; $result2 = mysql_query($query2) or die("Select Error :" . mysql_error()); $numofrows2 = mysql_num_rows($result2); for($i = 0; $i<$numofrows2; $i++){ $row2 = mysql_fetch_array($result2); echo "<tr><td><a href='display_forum.php?id={$row2[section_sub_id]}'>".stripslashes($row2[section_sub_title])."</a></td><td>"; $new = mysql_query("Select * FROM posts WHERE section_sub_id = '".$row2['section_sub_id'] . " ' ") or die ("Select Error :" . mysql_error()); //this is where the error message is from $lastpost = mysql_fetch_assoc($new); echo $lastpost['username']; echo"</td></tr>"; } } i've checked that the names of the tables and fields in the queries are correct, but i cant think of anything else that might be wrong. any help would be great. Hey folks, Sorry for being a pain in the ass. I am trying to submit data to my database via a form and when I click Submit, I get: Duplicate entry '' for key 1 I understand that it means I have a duplicate entry with the ID of 1 or something like that. I can't find where the issue is. Here is the form: <form actin="" id="settings" name="settings"> <table class="listing form" cellpadding="0" cellspacing="0"> <tr> <th class="full" colspan="2"><?php echo $lang_settings; ?></th> </tr> <tr> <th colspan="2"><?php echo $lang_settings_description; ?></th> </tr> <tr> <td><?php echo $lang_sitename; ?>: </td> <td><input type="text" name="sitename" value="<?php echo $site_name; ?>" width="172" /> <em>Site name for logo</em></td> </tr> <tr> <td><?php echo $lang_email; ?>: </td> <td><input type="text" name="email" value="<?php echo $site_email; ?>" width="172" /> <em>Your email address</em></td> </tr> <tr> <td><?php echo $lang_yourname; ?>: </td> <td><input type="text" name="name" value="<?php echo $your_name; ?>" width="172" /> <em>Your own name</em></td> </tr> <tr> <td><?php echo $lang_meta_description; ?>: </td> <td><input type="text" name="meta-description" value="<?php echo $description; ?>" width="172" /> <em>SEO</em></td> </tr> <tr> <td><?php echo $lang_keywords; ?>: </td> <td><input type="text" name="meta-keywords" value="<?php echo $keywords; ?>" width="172" /> <em>Separate with Commas</em></td> </tr> <tr> <td><input type="submit" class="button" name="submit" value="<?php echo $lang_button_savesettings; ?>"></td> </tr> </table> </form> Here is the Insert code: $insert = "INSERT INTO settings (site_name, description, keywords, email, name) VALUES ('$sitename', '$meta_description', '$meta_keywords', '$site_email', '$your_name')"; mysql_query($insert) or die(mysql_error()); Can anyone please tell me where I am going wrong here? Much appreciated. Hello there, I have some code here which sends a number of variables from flash to SQL... I would simply like to add the functionality to overwrite records which have the same 'name' or 'pseudo'... can anyone help me please ? Thanks in advance Martin <?php $pseudo=$_POST['var1']; $score=$_POST['var2']; $table = $_POST['tab']; $dategame = $_POST['tempjoueur']; //$micro = microtime(); //$dategame = time()."".substr($micro, 2, 6); $_COOKIE['User'] = $_SERVER['REMOTE_ADDR']; $envoie = InsertDatas($table, "name, score, dategame", "'".$pseudo."','".$score."','".$dategame."'"); if ($envoie) { print_r("OK, $pseudo, $score, $dategame,$ipclient"); } else { echo "BAD, $pseudo, $score, $dategame,$ipclient"; } ?> I have a code where i can edit or delete certain details from the database. Right now, if the user clicks on the edit button it takes him edit page where he can edit the details. But, I am not able to Incorporate a Delete button such that, when the user clicks on a delete button, it should ask for a confirmation box. If the user clicks YES, then do the following: Code: [Select] DELETE from emp WHERE emp_id='$emp_id'; When there are multiple entries and I click on delete it deletes everything from the database. how can i make it to delete only the entry that is besides the delete button? Code: [Select] if(mysql_num_rows($emp_query) > 0){ echo "<table border='1'>"; echo "<th>Employee Id </th>"; echo "<th>Employee Name </th>"; while($get_emp = mysql_fetch_assoc($emp_query)){ $emp_id = $get_emp['emp_id']; $emp_name = $get_emp['first_name']." ".$get_emp['last_name']; echo "<tr>"; echo "<td width='100'>"; echo $emp_id; echo "</td>"; echo "<td width='400'>"; echo $emp_name; $edit_path = 'edit_employee.php?id='.$emp_id; ?> <INPUT TYPE="button" style="display:inline;" value="VIEW / EDIT" onClick="location.href='<?php echo $edit_path; ?>'"> <form style='margin: 0; padding: 0; display:inline;' method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" onSubmit="return confirm('Are you sure this is correct?');"> <input style='display:inline;' name="delbutton" type="submit" value="DELETE"> <?php if(isset($_POST['delbutton'])){ $del_emp = mysql_query("DELETE from employee WHERE emp_id = '$emp_id'") or die(mysql_error()); //header('Location:view_employee.php'); } echo '</form>'; echo "</td>"; echo "</tr>"; } } Hi.. So im currently working on a script.. My script generates a "oid" based on timestamp. Ive made the "oid" field unique in my db, so if i do a quick refresh i get the message: Duplicate entry '1283195988' for key 'oid' Is there some way i can check if its a dublicate, and if it is + it with 1 or something? Hey guys! I know, I know this problem is EVERYWHERE but i just dont understand! I have a solid knowlage of php but my SQL skills are low, so i dont know too much about Keys and stuff. But my error is: Duplicate entry '' for key 2. The thing that im working on at this section is logging in with facebook. The code that presents my error is: $sql = "SELECT * FROM users WHERE uid=".$uid; $fbid = mysql_query($sql); $num_rows = mysql_num_rows($fbid); if(mysql_num_rows($fbid) < 1) { echo "You are not logged in. "; mysql_query("INSERT INTO `users` (`uid`) VALUES ('".$uid."')") or die(mysql_error()); } else { mysql_query("UPDATE users SET logged = '1' WHERE uid=".$uid); //mysql_query("UPDATE users SET full_name = $me WHERE uid=".$uid); echo "Your Logged in "; echo $me['name']; ?> Continue to <a href="removed :)"> My Settings </a>. <? } Any help is welcome For some reason this only allows one SQL to be added... // SQL Connection $username="monstert_admin"; $password="admin"; $database="monstert_admin"; $connection = mysql_connect("localhost", $username, $password) or die("Connection Failure to Database"); // Select Database mysql_select_db($database, $connection) or die ($database . "No Database" . $username); //Select everything from the the table $MyQuery = "SELECT * FROM photos"; $retrieve = mysql_query($MyQuery) or die(mysql_error()); if(mysql_num_rows($retrieve) != 0): $row = mysql_fetch_assoc($retrieve); else: echo ''; endif; if(isset($_POST['Submit']) && !$errors) { $url = $newname; include('img.php'); $image = new SimpleImage(); $image->load($url); $image->resize(500,315); $image->save($newname); mysql_query("INSERT INTO photos (url) VALUES ('$url')"); echo "File Uploaded Successfully as <i> "; echo $newname; echo "</i>"; } What would the issue be? I only have two columns - ID and url Thanks in advance! My query is not finding the last recieptnum entry, it is finding the number 9 everytime for some odd reason. Im trying to incrementally increase this each time a reciept is created. $getreceiptnum = mysql_query("SELECT receiptnum FROM accounting WHERE agency = '$agency' ORDER BY receiptnum DESC LIMIT 1") or die(mysql_error()); $recieptarray = mysql_fetch_array($getreceiptnum); $recieptnum = $recieptarray['receiptnum']; echo $recieptnum; I'd like some help understanding how to use validation for form field characters please! if(!preg_match("/^[a-zA-Z]+$/",$_POST['fname'])) { echo "Invalid Entry"; } Now, where do I specify what characters to check? Is it this part: ("/^[a-zA-Z]+$/",$_POST['fname']) How would I add other things? I believe that allows upper and lower case letters only, how would I add things like '@' '-' '_' etc etc...would I just add them to the list inside the square brackets or is it a new set of brackets?... What about numbers? Would it be "/[0-9]/" ? Also, can I swap $_POST['fname'] with just a plain variable ($fname)?. (oh and does it need the '/' inside each quote?) Thanks! OH and also! if I was to check several fields, and if all is ok execute a script would I need to have elseif/else statements of could I have a new if statement for each and then just run the script below it? Thanks again! Hello, i'm trying to query mysql to show table contents, my code so far is this: Code: [Select] mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM codes"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); to show content: Code: [Select] <?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"id"); $f2=mysql_result($result,$i,"code"); $f3=mysql_result($result,$i,"secret"); $f4=mysql_result($result,$i,"date"); $f5=mysql_result($result,$i,"ip"); ?> <tr> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td> </tr>Now this shows all the content from the table, what i want to have is 2 scripts which should work like this: Script1 to show table contents: ID1, ID2, ID4, ID5, ID7, ID8 and so on. Script2 to show table contents: ID3, ID6, ID9 and so on. Basically one that shows only every 3rd and skips 1 and 2, and another one that skips every 3rd. Is this possible ? Thanks ! I have a foreach loop as foreach ($xml->channel->item as $value){ $title = $value->title; $text = $value->text; echo "$title <br /> $text"; } I want to skip any entry in which the title contains a character like ":" I have a select box and I would like to select data click on it and I guess it should reload the page and populate the text boxes i have below with the data from the database. Below is the code so you can have an idea what i'm trying to do. Is this possible and help would be greatly appreciate. Thanks in advance. Code: [Select] <html> <head> <meta name="generator" content="PhpED Version 5.9.5 (Build 5989)"> <title> RSVP Administrator</title> <link rel="shortcut icon" href=""> </head> <body> <fieldset> <legend>Events</legend> <table> <tr> <td> Select an address: </td> <td> <SELECT NAME> <option value=' '> <? echo $option1 ?>;</option> </SELECT> </td> </tr> <tr> <td> Place of Event: </td> <td> <input type="text" name="location" value="<?php if($option1 == " "){ echo " ";} else{ echo $address_1;} ?>"> </td> </tr> <tr> <td> Address 1: </td> <td> <input type="text" name="address_1"> </td> </tr> <tr> <td> Address 2: </td> <td> <input type="text" name="address_2"> </td> </tr> <tr> <td> City: </td> <td> <input type="text" name="city"> </td> </tr> <tr> <td> State: </td> <td> <input type="text" name="state"> </td> </tr> <tr> <td> Zip Code: </td> <td> <input type="text" name="zip_code"> </td> </tr> <tr> <td> Telephone: </td> <td> <input type="text" name="phone"> </td> </tr> </table> </fieldset> </body> </html> Hi everyone, I need a little bit of help finishing off my code. Ive managed to get this far. Code: [Select] <?php mysql_connect("") or die ("Not Connected to MYSQL"); echo "</br>"; mysql_select_db("") or die ("Not Connected to DB"); // Database Connection stuff $partialNumber = $_POST['partialNumber']; // Post the Partial number $partialNumber = strtoupper($partialNumber); $numberSearch = mysql_query("SELECT * FROM product_option_value_description WHERE name LIKE '%$partialNumber%'") or die (mysql_error()); // Query to select the key number //Query to get product ID // $productId = "SELECT product_id FROM product_option_value_description"; //Query to get product ID // while ($keyNumber = mysql_fetch_array($numberSearch)) { $id = $keyNumber['product_id']; // Query for the images // $query = "SELECT image FROM product WHERE product_id = '$id'"; $result = mysql_query($query); $row = mysql_fetch_array($result) or die(mysql_error()); $query2 = "SELECT product_option_id FROM product_option_value WHERE product_id = '$id'"; $result2 = mysql_query($query2); $row2 = mysql_fetch_array($result2) or die(mysql_error()); $query3 = "SELECT product_option_value_id FROM product_option_value WHERE product_id = '$id'"; $result3 = mysql_query($query3); $row3 = mysql_fetch_array($result3) or die(mysql_error()); ?> <div> <br /><br /> Key Number: <? echo $keyNumber['name']; ?></a> <form action="http://www.co.uk/teststore/index.php?route=checkout/cart" method="post" enctype="multipart/form-data" id="product"> <br /> <table style="width: 100%;"> <tr> <td> Colour: <select name="option[<? echo $row2['product_option_id']; ?>]"> <option value="<? echo $row3['product_option_value_id']; ?>"></option> </select></td> </tr> </table> <div class="content"> Qty: <input type="text" name="quantity" size="3" value="1" /> <input type="hidden" name="product_id" value="<? echo $id; ?>" /> <input name="submit" type="submit" value="Add to Cart" /> </div> </form> <? echo $row3['product_option_value_id']; ?> </div> <br /> <img height="150" width="150" src='http://www.co.uk/teststore/image/<? echo $row['0']; ?>'/> <? } ?> And here is my SQL Table code. Code: [Select] product_option_value_id product_option_id product_id 599 302 49 598 302 49 589 297 42 588 297 42 So as you can probably tell, it is a search program that looks for products on a shopping cart. The products will have different option values, and the php script will grab the option values and echo them in a form to post back to the cart to add the product to the basket. The problem is that the "product_option_value_id" can have lots of different values, but my code echos only the first one it finds. So when I click the add to cart button, it will only add the first option value for the product it finds. For some reason I am having a hard time explaining this, so I hope someone can help me. Thanks for looking. I'm not sure if the window often seen being filled out, such as this one that I am writing into is php.
I'm looking for a window that will auto-size or has the ability to be expanded on demand like this window with the three lines on the bottom right corner.
Or to specify a size based on page properties like proportions or what is supposed to be typed into the field eg. character limit.
Is that php?
I'd be happy with a link of documentation or anything
Otherwise, currently I only have the basic fields that come up when using POST and GET
Thank you
Edited by greenace92, 05 December 2014 - 04:19 PM. Whenever I see a PHP website (and given ftp access) I often struggle to find the entry page. Most I see don't have index.php in the public_html directory - or that isn't the main entry page. Htaccess and adding Wordpress, shopping carts etc. also messes around with which file is the entry page. Is there a quick checklist of what a simpleton should do to find the entry page? E.g. (1) Check .htaccess................ etc. Hi I am getting this error message and don't know why. please help this is the message " Error: Duplicate entry '' for key 1 " Here is the code Code: [Select] <?php include'../DB-connection.php'; $sql="INSERT INTO Company(CompanyName,Address,Logo,PhoneNumber,ContactPerson) VALUES('".mysql_real_escape_string($_POST[CompanyName])."', '".mysql_real_escape_string($_POST[Address])."', '".mysql_real_escape_string($_POST[Logo])."', '".mysql_real_escape_string($_POST[PhoneNumber])."', '".mysql_real_escape_string($_POST[ContactPerson])."')"; if(!mysql_query($sql,$con)) { die(' Error: '. mysql_error()); } echo " <center>your compnay info added       <a href='User-Login.php'>Login to Post a job</a> </center>" ; mysql_close($con) ?> |