PHP - Update Function With Array Of Authors
Hi,
I have the following code which basically checks an array of input textboxes and inserts them into a database table: public function insertAuthor($authArray, $PCorder=0) { $query = sprintf('SELECT Pid, Pname FROM People WHERE Pname IN(\'%s\') ORDER BY Pid ASC', implode('\',\'', $authArray)); $result = mysql_query($query); $maxquery = "SELECT MAX(CPid) as max FROM ConfPaper WHERE CPRid = ".$_GET['CPRid']; $maxresult = mysql_query($maxquery); $max = mysql_fetch_array($maxresult); $CPid = $max['max']; if($result && mysql_num_rows($result) > 0) { $sqlValues = array(); while(list($PId, $PName) = mysql_fetch_row($result)) { if(in_array($PName, $authArray)) { $sqlValues[] = sprintf("(%d, 1, ".$CPid.", %d, now(), 0)", $PId , $PCorder++ ); // Author already exists within the Pname table // remove user from $authArray $key = array_search($PName, $authArray); unset($authArray[$key]); } } $sql = "INSERT INTO PeopleCon(Person_id, PCHid, Pid, PCorder, PCdateadded, PCdeleted) VALUES \n"; $sql .= implode(",\n", $sqlValues); $result = mysql_query($sql); } // If there are Authors left within the $authArray // Add them to the Pname table if(count($authArray) > 0) { People::insertPersons($authArray); // call insertPersons method for remaining authors $this->insertAuthor($authArray, $PCorder); // insert the remaining auhtors into PeopleCon } } insertPersons method is as such: public function insertPersons($PName) { $query = "INSERT INTO People (Pname) VALUES ('" . implode("'), ('", $PName) . "')"; $result = mysql_query($query); } The logic works by firstly checking to see if an Author exists, if so they are inserted into PeopleCon, if not they are inserted into People AND PeopleCon. This all works perfectly.. But now what i need to do is build an update method where users can edit the authors also. The logic would be more or less the same, but i wouldn't neeed an insertpersons method, i would need to change it, and also the script would edit the existing authors, and if anymore are added apply the same method as above. Can anyone help me out as to how i should go about tackling this? Thanks Kind regards Billy Similar TutorialsIs it a good idea to reuse say a database file someone else made, or should you try to make your own? Can anyone post a generic update function to update mysql table. The manual approach: update $tablename set $column1='a', $column2='b' where $id=$value; Hi Guys, I want my UPDATE function from the code below to only update those fields that are NOT empty - if they are empty on submit then do not update or change any values from that field. I know that sounds odd, but with my file fields, when I submit to change something else at a later date it will overwrite the photo, download1,2 & 3 values and as a result I loose my files from the mysql table. Cheers, S <?php include('config.php'); if (isset($_GET['Ter']) ) { $ter = (int) $_GET['Ter']; if (isset($_POST['submitted'])) { //Photo & Document Upload Upload $timestamp_photo = time(); $timestamp_download1 = time(); $timestamp_download2 = time(); $timestamp_download3 = time(); //This is the directory where the files will be saved //Photos $photo_target = "images/"; $photo_target = $photo_target .$timestamp_photo. basename( $_FILES['photo']['name']); //Documents $download_target = "documents/"; $download_target1 = $download_target .$timestamp_download1. basename( $_FILES['download1']['name']); $download_target2 = $download_target .$timestamp_download2. basename( $_FILES['download2']['name']); $download_target3 = $download_target .$timestamp_download3. basename( $_FILES['download3']['name']); //This gets all the other information from the form $photo = ($_FILES['photo']['name']); $download1 = ($_FILES['download1']['name']); $download2 = ($_FILES['download2']['name']); $download3 = ($_FILES['download3']['name']); //Pause Photo/Document Upload foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); } $sql= "UPDATE `ter` SET `Ter` = '{$_POST['Ter']}' , `BranchName` = '{$_POST['BranchName']}' , `BranchAddress` = '{$_POST['BranchAddress']}' , `BranchTel` = '{$_POST['BranchTel']}' , `BranchEmail` = '{$_POST['BranchEmail']}' , `BranchLink` = '{$_POST['BranchLink']}' , `Theme` = '{$_POST['Theme']}' , `LocalInfo` = '{$_POST['LocalInfo']}' , `BranchInfo` = '{$_POST['BranchInfo']}' , `photo` = '$timestamp_photo{$_FILES['photo']['name']}' , `download1` = '$timestamp_download1{$_FILES['download1']['name']}' , `download1name` = '{$_POST['download1name']}' , `download2` = '$timestamp_download2{$_FILES['download2']['name']}' , `download2name` = '{$_POST['download2name']}' , `download3` = '$timestamp_download3{$_FILES['download3']['name']}' , `download3name` = '{$_POST['download3name']}' WHERE `Ter` = '$ter' "; mysql_query($sql) or die(mysql_error()); //Unpause Photo/Document Upload //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $photo_target)) { echo "<br />The file ".$timestamp_photo. basename( $_FILES['photo']['name']). " has been uploaded. <br />"; } else { echo ""; } //End of Photo/Document Upload //Writes the photo to the server if(move_uploaded_file($_FILES['download1']['tmp_name'], $download_target1)) { echo "<br />The file ".$timestamp_download1. basename( $_FILES['download1']['name']). " has been uploaded. <br />"; } else { echo ""; } //End of Photo/Document Upload //Writes the photo to the server if(move_uploaded_file($_FILES['download2']['tmp_name'], $download_target2)) { echo "<br />The file ".$timestamp_download2. basename( $_FILES['download2']['name']). " has been uploaded. <br />"; } else { echo ""; } //End of Photo/Document Upload //Writes the photo to the server if(move_uploaded_file($_FILES['download3']['tmp_name'], $download_target3)) { echo "<br />The file ".$timestamp_download3. basename( $_FILES['download3']['name']). " has been uploaded. <br />"; } else { echo ""; } //End of Photo/Document Upload echo (mysql_affected_rows()) ? "<br />Edited Branch.<br />" : "<br />Nothing changed. <br />"; } $row = mysql_fetch_array ( mysql_query("SELECT * FROM `ter` WHERE `Ter` = '$ter' ")); ?> hi i am trying to make a payroll calculator script that takes employee info, calculates pay, displays submitted info in a table, stores info in an array, and updates the array when new info is submitted. i have most of these accomplished, i am having trouble with the "store into an array, and update the array when new info is submitted" parts of the project. i am still not very fluent in php so there may be easier ways to achieve what i have so far. any pointers would be a great help, this part has got me stumped. I've used a lot of solutions from this site, however, this is my first post, so go easy on me :-) I've created a product form the successfully INSERTS data to a mysql table. I can successfully query the data so that product information can be updated. When I click submit to UPDATE the rows, everything updates except for 2 fields that are arrays. Here is my product form that populates data to be edited. Code: [Select] <?php session_start(); require("config.php"); require("db.php"); require("functions.php"); if(isset($_SESSION['SESS_ADMINLOGGEDIN']) == FALSE) { header("Location: " . $config_basedir); } if(isset($_GET['func']) == TRUE) { if($_GET['func'] != "conf") { header("Location: " . $config_basedir); } $validid = pf_validate_number($_GET['id'], "redirect", $config_basedir); header("Location: " . $config_basedir . "adminedit.php"); } else { require("header.php"); $sql = "SELECT * FROM products WHERE id = " . $_GET['id'] . ";"; $query = mysql_query($sql); while ($prodrow = mysql_fetch_array($query)){ $id = $prodrow['id']; $active=$prodrow['active']; $cat_id=$prodrow['cat_id']; $name=$prodrow['name']; $description=$prodrow['description']; $details=$prodrow['details']; $price=$prodrow['price']; $price2=$prodrow['price2']; $price3=$prodrow['price3']; $price4=$prodrow['price4']; $price5=$prodrow['price5']; $price6=$prodrow['price6']; $minimum=$prodrow['minimum']; $price7=$prodrow['price7']; $price8=$prodrow['price8']; $price9=$prodrow['price9']; $image=$prodrow['image']; } ?> <form action="adminprodupdate.php" method="post"> <table width="500" border="1" cellpadding="10"> <tr> <td>ID:</td> <td><input type="hidden" value="<? echo $id; ?>" name="id" /></td> </tr> <tr> <td>Active:</td> <td><input name="active" type="radio" value="0" <?php echo ($active=='0')? 'checked':'';?> />Yes<br /> <input name="active" type="radio" value="1" <?php echo ($active=='1')? 'checked':'';?> />No</td> </tr> <tr> <td>Category:</td> <td><input name="cat_id" type="radio" value="1" <?php echo ($cat_id=='1')? 'checked':'';?> />Hats<br /> <input name="cat_id" type="radio" value="2" <?php echo ($cat_id=='2')? 'checked':'';?> />Shirts<br /> <input name="cat_id" type="radio" value="3" <?php echo ($cat_id=='3')? 'checked':'';?> />Promotional Items</td> </tr> <tr> <td>Product ID: </td> <td><input type="text" name="name" value="<? echo $name; ?>"></td> </tr> <tr> <td>Short Description: </td> <td><input type="text" name="description" value="<? echo $description; ?>"></td> </tr> <tr> <td>Details:</td> <td><textarea name="details" cols="50" rows="10" ><? echo $details; ?></textarea></td> </tr> <tr> <td>Price for S-XL:<br /> <br /><strong><div id="redfont">**Shirts**<br />&<br />**Hats**</div></strong></td> <td>1-23: <input type="text" name="price" value="<? echo $price; ?>"><br><br> 24-47: <input type="text" name="price2" value="<? echo $price2; ?>"><br><br> 48+: <input type="text" name="price3" value="<? echo $price3; ?>"><br><br> </td> </tr> <tr> <td>Price for 2XL - 5XL:<br /> <br /><strong><div id="redfont">**Shirts ONLY!**</div></strong></td> <td>1-23: <input type="text" name="price4" value="<? echo $price4; ?>"><br><br> 24-47: <input type="text" name="price5" value="<? echo $price5; ?>"><br><br> 48+: <input type="text" name="price6" value="<? echo $price6; ?>"><br><br> </td> </tr> <tr> <td>Minimum QTY: </td> <td><input type="text" name="minimum" value="<? echo $minimum; ?>"></td> </tr> <tr> <td>Sizes:</td> <td><? echo ''; $result = mysql_query("SELECT * FROM sizes"); while ($row = mysql_fetch_assoc($result)) { $selected = ''; $result2 = mysql_query("SELECT * FROM productoptions WHERE productid = '" . $_GET['id'] . "' AND sizeid = '" . $row['id'] . "'"); if ($row2 = mysql_fetch_assoc($result2)) { $selected = 'checked '; } echo '<input type="checkbox" name="size[]" value="' . $row['id'] . '" ' . $selected . '/> ' . $row['size'] . '<br />'; } ?> </td> </tr> <tr> <td>Colors:</td> <td><? $result = mysql_query("SELECT * FROM colors"); while ($row = mysql_fetch_assoc($result)) { $selected = ''; $result2 = mysql_query("SELECT * FROM productoptions WHERE productid = '" . $_GET['id'] . "' AND colorid = '" . $row['id'] . "'"); if ($row2 = mysql_fetch_assoc($result2)) { $selected = 'checked '; } echo '<input type="checkbox" name="color[]" value="' . $row['id'] . '" ' . $selected . '/> ' . $row['color'] . '<br />'; } ?> </td> </tr> <tr> <td> </td> <td><input type="Submit" value="Update this product"></td> </tr> </form> </table> <?php } require("footer.php"); ?> Here is my script: Code: [Select] <?php include("config.php"); include("db.php"); $id=$_POST['id']; $active=$_POST['active']; $cat_id=$_POST['cat_id']; $name=$_POST['name']; $description=$_POST['description']; $details=$_POST['details']; $price=$_POST['price']; $price2=$_POST['price2']; $price3=$_POST['price3']; $price4=$_POST['price4']; $price5=$_POST['price5']; $price6=$_POST['price6']; $minimum=$_POST['minimum']; $arrsizes = $_POST['size']; $arrcolors = $_POST['color']; $image=$_POST['image']; $sql="UPDATE products SET active = '$active', cat_id = '$cat_id', name = '$name', description = '$description', details = '$details', price = '$price', price2 = '$price2', price3 = '$price3', price4 = '$price4', price5 = '$price5', price6 = '$price6', minimum = '$minimum', image = '$image' WHERE id = '$id'"; $result = mysql_query("SELECT id FROM products ORDER BY id DESC LIMIT 0,1"); if ($row = mysql_fetch_assoc($result)) { $productid = $row['id']; } foreach ($arrsizes as $sizevalue) { foreach ($arrcolors as $colorvalue) { $sql2="UPDATE productoptions SET sizeid = '$sizevalue', colorid = '$colorvalue' WHERE productid='$id' "; } } mysql_query($sql) or die ("Error: ".mysql_error()); mysql_query($sql2) or die ("Error: ".mysql_error()); echo $sql; echo $sql2; header("Location: " . $config_basedir . "adminhome.php"); ?> And here is what is echo'd after submit: Code: [Select] UPDATE products SET active = '0', cat_id = '2', name = 'Test Edit Prod', description = 'just a simple test', details = 'just a simple testjust a simple testjust a simple testjust a simple testjust a simple testjust a simple test', price = '1', price2 = '2', price3 = '3', price4 = '4', price5 = '5', price6 = '6', minimum = '127', image = '' WHERE id = '41'UPDATE productoptions SET sizeid = '6', colorid = '5' WHERE productid='41' This part of the code: UPDATE productoptions SET sizeid = '6', colorid = '5' WHERE productid='41' , is supposed to be updating sizes and colors, however, it is only updating the last size/color selected in the check box. Thanks in advance for any support! I am having some problems getting a query correct. Basically I have two tables, one with listings and another with categories. In the listings table I have a column called shortdescription. I am trying to pull the shortdescription from the listings table with the query $shortdesc = array_shift(mysql_fetch_row(mysql_query("select shortdescription from links where category = '".$scat->id."' "))); The shortdecription display properly on the pages display the listings, however I am getting the following error on any pages that only display the parent categories Warning: array_shift() [function.array-shift]: The argument should be an array in /home/...path/file.php on line 1462 The listings id numbers begin at 75+ because the initial parent category id ends at 74. The query seems to be searching for listing ids below 75 and spitting out an error because it is not finding them. Any ideas on how to eliminate this error and/or stop the query from looking for non-existant data? I'm new to function and I can't figure how to update/result the score. I developed a game called Rock, Paper, & Scissors, so I can learn the function itself with a simple game for now. http://pastebin.com/9MB7ATnt Any tips or instruction would be much appreciated. Thanks. Hi, i have a user update function in my code so I can easily change user fields int he database public function updateUser ($username, $value, $what) { $q = "UPDATE `users` SET ? = ? WHERE username = ?"; if ($stmt = $this->db_connection->prepare($q)) { $stmt->bind_param("sis", $what, $value, $username); $stmt->execute(); } } The database is connected successfully, but say i run a updateUser('blaine0002', 'blah', 'password'); nothing would get updated and no errors are thrown. Am i doing something wrong? Thank you! I am trying to create a CMS management website, but I can't seem to get the update function to work. Everything else works fine but not the update function. Can anyone please tell me why or what the problem is? I have spent too long trying to fix it and have failed. It is correctly linked to the database when I hit the edit button all i get is UPDATE_CONTENT_FORM($_GET['ID'])?>where the text boxes should be is . Please help, I am really stuck. (sorry about spelling ) code in CMS_Class.php Class modernCMS{ var $host='localhost'; var $username='lmcmanus13'; var $password='k0gl0zfh3g1ccm4v'; var $db='lmcmanus13'; function connect(){ $con = mysql_connect($this->host, $this->username, $this->password); mysql_select_db($this->db,$con); } function get_content($id =''){ if ($id != ""): $id = mysql_real_escape_string($id);//helps to protect database from beening hacked $sql = "SELECT * FROM `CMS_Content` WHERE id ='$id'"; else: $sql = 'SELECT * FROM `CMS_Content` WHERE 1'; endif; //$query = 'SELECT * FROM `CMS_Content` WHERE 1'; $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result)!=0): while($row= mysql_fetch_assoc($result)){ echo '<h1><a href="Animals.php?id=' . $row['id'] . '">' . $row['Title'] . '</h1>'; echo '<p>' . $row['Body'] . '</p>'; } else: echo '<p> we are sorry there seems to be a problem with your request</p>'; endif; echo $return='<p><a href ="Animals.php">Back</a></p>'; } function add_content($_POST){ $Title= mysql_real_escape_string($_POST['Title']); $Body= mysql_real_escape_string($_POST['Body']); if(! $Title || ! $Body): if(!$Title=""): echo"<p>The Title is required<p>"; endif; if(!$Body=""): echo"<p>The Body is required<p>"; echo '<a href="add-content.php">Try Again</a>'; endif; else: $sql="INSERT INTO `CMS_Content`(`id`, `Title`, `Body`) VALUES ('null','$_POST[Title]','$_POST[Body]')"; $result = mysql_query($sql) or die(mysql_error()); echo "<meta http-equiv='refresh' content='0;url=added.php'>"; endif; } function manage_content (){ echo '<div id ="manage">'; $sql = 'SELECT * FROM `CMS_Content`'; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_assoc($result)): echo '<h1><a id=' . $row['id'] . '">' . $row['Title'] . '</h1>' ?> <div> <span ><a href="update-content.php?id=<?php= echo= $row['id']?>">Edit</a>|<a href="?delete=<?php echo $row['id']; ?>">Delete</a></a></span> </div> <?php endwhile; echo '</div>';//closes the manages div } Function delete_content($id){ if(!$id){ return false; }else{ $id=mysql_real_escape_string($id); $sql="DELETE FROM CMS_Content WHERE id='$id'"; $result = mysql_query($sql) or die(mysql_error()); echo "<meta http-equiv='refresh' content='0;url=deleted.php'>"; } function update_content_form($id) { $id = mysql_real_escape_string($id); $sql = "SELECT * FROM CMS_Content WHERE id = '$id'"; $res = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($res) ?> <form action="Animals.php" method="post" > <input type="hidden" name="update" value="true" /> <input type="hidden" name="id" value="<?php=$row['id']?>" /> <div> <label for="title">Title:</label> <input type="text" name="Title" id="Title" value="<?php=$row['Title']?>" /> </div> <div> <label for="body">Body:</label> <textarea name="body" id="body" rows="8" cols="40"><?php=$row['Body']?></textarea> </div> <input type="submit" name="submit" value="Update content" /> </form> <?php function update_content($p) { $title = mysql_real_escape_string($s['title']); $body = mysql_real_escape_string($s['body']); $id = mysql_real_escape_string($p['id']); if(!$title | !$body): if(!$title): echo "<p>The Title is Required</p>"; endif; if(!$body): echo "<p>The body is Required</p>"; endif; echo '<p><a href=" update_content.php?id=' . $id . '">Try Again</a></p>'; else: $sql = "UPDATE CMS_Content SET title = '$title', body = '$body' WHERE id = '$id'"; $res = mysql_query($sql) or die(mysql_error()); echo "Updated Successfully!"; endif; } } }//end of class } ?>code in Animals.php <h1> Our Animals </h1> <ul> <li><a href="manage-content.php">Manage Content</a></li> <li><a href="add-content.php">Add Content</a></li> </ul> <?php if(isset($_POST['add'])): $obj->add_content($_POST); elseif(isset($_POST['update'])): $obj->update_content_form($_POST); endif; ?> Code in update-content.php <h1> Our Animals j,j</h1> <h1> Update Content </h1> <?=$obj->update_content_form($_GET['id']) ?> Edited by Ch0cu3r, 10 December 2014 - 11:47 AM. Added code tags Is there any way to tell my php ajax file to run the update query if the data already exist and if not, then create the row in the database? I have both the update and the insert functions created, but was just wondering if I could tell php which one to use without passing through a parameter. Hi, Ok, so I'm making a registration form system, where I can create new registration forms for the users to pick a date etc. In my form I can add and remove text fields via Javascript - to add additional dates etc. so I don't know if I'll be submitting 3 fields or 15. The could look like this: <input type="hidden" name="session_id[]" value="<?php echo $session_id; ?>"> <input type="text" name="picked_date[]"> <input type="text" name="picked_room[]"> They are put in a big array: De bliver smidt i et stort array: $n=0; foreach ($session_id as $_session_id) { $bigar[$n][1] = $_session_id; $n++; } $n=0; foreach ($picked_date as $_picked_date) { $bigar[$n][2] = $_picked_date; $n++; } $n=0; foreach ($picked_room as $_picked_room) { $bigar[$n][3] = $_picked_room; $n++; } $n=0; which is then updated in the database using: foreach ($bigar as $part) { mysql_query("UPDATE... } Right - so far so good. The updating works fine with existing fields, but if I remove some of the fields (via JS) it obviously only updates the remaining in the array. The left out will be ignored. On the same note I would like to add fields as well, so - the bottom line - I want to: - UPDATE rows, where the ID is in the array - DELETE existing rows, WHERE the ID is NOT in the array - CREATE new rows for the new fields in the array (not having an ID) How can this be achieved? Hope someone can help... hey guys im really stuck and need help with this. im trying to update a mysql table from an array. basically i have 12 records what are pulled using a query. each record has its own ID to distinguish it from the rest. i then have 2 tick boxes for the user to select one for accept and one for deny. what i need is for the query to look at each record on the table and insert the selection boxes value if ticked. my code is below but i have altered it to the hilt and cannot get it to work so please help Code: [Select] <?php $path = $_SERVER['DOCUMENT_ROOT']; $path .= "/Apollo/dbc.php"; include_once($path); $rs_results = mysql_query("SELECT * FROM off WHERE IsItAuthorised='0' and isitsick='0' ORDER BY DayOff"); ?> <html> <head> <title>Administration Main Page</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <?php if (isset($_POST['submit'])) { //Assign each array to a variable $id = $_POST['id']; $approve = $_POST['approve']; $deny = $_POST['deny']; $limit = count($id); $values = array(); // initialize an empty array to hold the values for($k=0;$k<$limit;$k++){ $msg[] = "$limit Holidays Updated"; $id[k] = $id; $approve[k] = $approve; $deny[k] = $deny; $id = implode($id); $approve = implode( $approve); $deny = implode( $deny); $query = "UPDATE off SET Authorised = $approve , Deny = $deny WHERE OffID = $id " ; } $Event = "INSERT INTO events (UserName, Event ) VALUES ('$_SESSION[user_name]', 'Entered New KPI' )"; echo $query; if (!mysql_query($query,$link)){ die('Error: ' . mysql_error()); } else { mysql_query($Event); echo "<div class=\"msg\">" . $msg[0] . "</div>"; } } ?> </head> <body> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="14%" valign="top"><?php ?> </td> <td width="74%" valign="top" style="padding: 10px;"> <p><?php if(!empty($msg)) { echo $msg[0]; } ?></p> <p> <?php $cond = ''; $sql = "select * from off "; $rs_total = mysql_query($sql) or die(mysql_error()); $total = mysql_num_rows($rs_total); ?> <p> <form name "searchform" action="/Apollo/Admin/HolidayRequests.php" method="post"> <table width="100%" border="0" align="center" cellpadding="2" cellspacing="0"> <tr class="mytables"> <td width="4%"><font color="white"><strong>ID</font></strong></td> <td width="4%"> <font color="white"><strong>Staff Member</font></strong></td> <td width="10%"><font color="white"><strong>Day Off</font></strong></div></td> <td width="10%"><font color="white"><strong>Is It Authorized</font></strong></div></td> <td width="15%"> </td> </tr> <tr> <td> </td> <td width="10%"> </td> <td width="17%"><div align="center"></div></td> <td> </td> </tr> <?php while ($rrows = mysql_fetch_array($rs_results)) {?> <tr> <td><input name="id[]" id="id[]" size="4" value="<?php echo $rrows['OffID'];?>" /></td> <td><?php echo $rrows['StaffMember']; ?></td> <td><?php echo date('d/m/Y', strtotime($rrows['DayOff']));?></div></td> <td> <span id="approve<?php echo $rrows['id']; ?>"> <?php if(!$rrows['IsItAuthorised']) { echo "Pending"; } else {echo "Authorized"; }?> </span> </td> <td> <input type="hidden" name="approve[]" id="approve[]" value="0"> <input type="checkbox" name="approve[]" id="approve[]" value="1"> Approve <input type="hidden" name="deny[]" id="deny[]" value="0"> <input type="checkbox" name="deny[]" id="deny[]" value="1"> Deny </td> </tr> <?php } ?> </table> <input name="submit" type="submit" id="submit" value="Submit"> </form> </p> <?php ?> <p> </p> <p> </p> <p> </p> <p> </p></td> <td width="12%"> </td> </tr> </table> </body> </html> Hi all, Im having some trouble trying to update my table using an array, It processes and doesnt throw up any errors so im at a loss as to whats happening as its not updating my table? my page is Code: [Select] mysql_select_db($database_saucy_connection, $saucy_connection); $query_allphotos = "SELECT * FROM model_login, model_pictures WHERE model_pictures.user_id=model_login.id"; $allphotos = mysql_query($query_allphotos, $saucy_connection) or die(mysql_error()); $row_allphotos = mysql_fetch_assoc($allphotos); $totalRows_allphotos = mysql_num_rows($allphotos); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <table width="500" border="0" cellspacing="1" cellpadding="0"> <form name="form1" method="post" action=""> <tr> <td> <table width="500" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center"><strong>Id</strong></td> <td align="center">Picture Name</td> <td align="center">Upload Date</td> <td align="center"><strong>No Downloads</strong></td> <td align="center"> Approved? </td> <td align="center"> Hunnies Gallery </td> <td align="center"> Hunks Gallery </td> <td align="center"> Default Pic </td> <td align="center"> Theme Pic </td> <td align="center"> Homepage Pic </td> </tr> <?php while($rows=mysql_fetch_array($allphotos)){ ?> <tr> <td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td> <td align="center"><? $user_picture[]=$rows['user_picture']; ?><?php echo $rows['user_picture']; ?></td> <td align="center"><? $user_picture_date[]=$rows['user_picture_date']; ?><?php echo $rows['user_picture_date']; ?></td> <td align="center"><? $picture_downloads[]=$rows['picture_downloads']; ?><?php echo $rows['picture_downloads']; ?></td> <td align="center"><? $user_pic_approval[]=$rows['user_pic_approval']; ?><?php echo $rows['user_pic_approval']; ?> </td> <td align="center"><? $hotties_gallery[]=$rows['hotties_gallery']; ?><?php echo $rows['hotties_gallery']; ?> </td> <td align="center"><? $hunks_gallery[]=$rows['hunks_gallery']; ?><?php echo $rows['hunks_gallery']; ?> </td> <td align="center"><? $default_pic[]=$rows['default_pic']; ?><?php echo $rows['default_pic']; ?> </td> <td align="center"><? $theme_gallery[]=$rows['theme_gallery']; ?><?php echo $rows['theme_gallery']; ?> </td> <td align="center"><? $homepage=$rows['homepage_pic']; ?><?php echo $rows['homepage_pic']; ?> </td> </tr> <?php } ?> <tr> <td colspan="5" align="center"><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </td> </tr> </form> </table> <?php // Check if button name "Submit" is active, do this if($Submit){ for($i=0;$i<$count;$i++){ $sql1="UPDATE $model_pictures SET user_pic_approval='$user_pic_approval[$i]', hotties_gallery='$hotties_gallery[$i]', hunks_gallery='$hunks_gallery[$i]', default_pic='$default_pic[$i]', theme_gallery='$theme_gallery[$i]', homepage_pic='$homepage_pic[$i]' WHERE id='$id[$i]'"; $result1=mysql_query($sql1); } } How can I use php to update a dynamic amount of input fields and tell mysql which ones to update respectively? Essentially I have a list of occupations and if someone wants to update one of them I don't know how to tell php which one they've changed in a dynamic way. Code: [Select] <?php if(!empty($occupations)) { ?> <h3 id="job_function">Job Functions</h3> <?php $i = 0; foreach($occupations as $occupation) { ?> <div class="formElement"> <div class="formFieldLabel "> <label for="occupation_<?php echo $i; ?>"><?php echo $occupation['companyname']; ?></label> </div> <div class="formField"> <input id="occupation_<?php echo $i; ?>" type="text" name="occupation_<?php echo $i; ?>" value="<?php echo $occupation['function']; ?>" /> </div> </div> <?php $i++; } } ?> Hi all, I'm trying to figure out how to update values I have in an array. I'm a bit stuck and hope someone can advise. I've read about all sorts of bits but I'm stuck, I dont really understand associative so thought I'd post an example of my code for someone to hopefully assist.. so, if I have a table $fees, which is filled from a select, for example.. select * from `cases_fees`. "contains fields for id, type, amount and others now I want to do is foreach through that table and update a field called `amount`. but I cant understand the foreach >= as key part. what is a key and what isn't ? (I know what the keys of the db table arem but that's not the same eh?) I'm fine with normal foreach, but I cant update back to the array so I need to find out how to be able to set valus back into $fees[`amount`] hope someone can point me in the right direction please... Ted Hi,
I've seen this done, and I thought it would be simple now that I am starting to understand ajax but I can't figure it out.
I am using jQuery ajax via wordpress. I can click a button, call a php function and use the value returned from php to populate the textbox.
Ok, great!
but I want to run a rather lengthy procedure, and while it's running I would like it to record it's status in a multi-line text box as sort of a debugger ( and to pass the time while waiting )
How can I accomplish this?
I've googled but I must not be using the correct words to search.
I've tried a few different methods, but I can't get this damn function to work. Can someone show me where I've gone wrong? Here's my code & form. I'm still baffled why it doesn't work! :lol: url is update-content.php?id=12 <?php include("../include/session.php"); if(!$session->logged_in) { header('Location: ../login.php'); die; } require_once('../include/functions.php'); function content($id) { $id = $_GET['id']; $connection = db_connect(); $query = sprintf("select * from content where id = '$id'", mysql_real_escape_string($id) ); $result = mysql_query($query); $number_of_posts = mysql_num_rows($result); if ($number_of_posts == 0) { return false; } $row = mysql_fetch_array($result); return $row; } function update($param) { // Get the content id from url to output into the editor $id = $_GET['id']; $connect = db_connect(); $page = mysql_real_escape_string($_POST['page']); $page_id = mysql_real_escape_string($_POST['page_id']); $title = mysql_real_escape_string($_POST['title']); $text = mysql_real_escape_string($_POST['text']); $query = ("UPDATE content SET page = '$page' page_id = '$page_id' title = '$title' text = '$text' WHERE id = '$id' "); $result = mysql_query($query); if (!$result) { return false; } else { return true; } } ?> <?php // Send form data to mysql if(isset($_POST['text'])) { $result = update($_POST); if($result === true) { echo 'Success!'; die(); } } ?> And the form.. <form form name="editor" id="editor" method="post" action="update-content.php"> <p><br /> <b>Assign to page</b><br /> <input name="page" id="page" size="60" maxlength="500" value="<?php $page = content($id); echo stripslashes($page['page']); ?>" /> <br /> <br /> <b>Page id</b> <input name="page_id" id="page_id" size="4" maxlength="4" value="<?php $page_id = content($id); echo stripslashes($page_id['page_id']); ?>" /> <br /> <br /> <b>Title</b> <span class="smalltext">(Just a short name this piece of content)</span><br /> <input name="title" id="title" size="60" maxlength="200" value="<?php $title = content($id); echo stripslashes($title['title']); ?>" /> <br /> <br /> <br /> <strong>Content</strong> <span class="smalltext">(paste html in here)</span><br /> <textarea name="text" id="text" cols="75" rows="15"><?php $text = content($id); echo stripslashes($text['text']); ?></textarea> <?php //turn the text area into CK Editor echo $ckeditor_ini; ?> <br /> <input type="image" src="../images/button_submit.gif" alt="submit" name="submit" value="submit" /> <a href="index.php"><img src="../images/button_cancel.gif" alt="Cancel" width="120" height="26" border="0" /> </form> I want to define a function instead of repeating query in all my php pages. I call a function by passing an $id value and from that function i have to get all the info related to that id, like name, description and uom.
I am trying to do this, but i dont know how to get these values seperately.
here is my function
function items($item_id) { $details = array(); $result = mysql_query("select item_id, name, uom, description from items where item_id=".$item_id."") or die (mysql_error()); while($row = mysql_fetch_array($result)) { $details[] = array((stripslashes($row['name'])), (stripslashes($row['uom'])), (stripslashes($row['description']))); } return $details; }and i call my function like this $info = items($id);Can somebody guide me in this I have the below code: Code: [Select] <div> <table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr> <td colspan="7" valign="top"> <form enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF']; ?>" method="post"> <?php if (isset($submit)) { // UPDATE QUERY CODE WHEN SUBMIT IS ENTERED $str = implode(';',$_POST['checks']); $insert = "UPDATE members SET settings='$str' WHERE member_id='$_SESSION[SESS_MEMBER_ID]'"; if (@mysql_query($insert)) { ?> <script type="text/javascript"> document.location.replace('settings-tests.php'); </script> <?php } else { echo('Error in you submission:' . mysql_error() . "<br /><br />" . $sql); } } ?> <?php { $result=mysql_query("SELECT * FROM members WHERE member_id = '$_SESSION[SESS_MEMBER_ID]'"); $row = mysql_fetch_array($result); $arr = array(); $arr = explode(';',$row['settings']); $member_id = $row['member_id']; $firstname = $row['firstname']; $lastname = $row['lastname']; $email = $row['email']; $membership = $row['membership']; $roles = $row['roles']; $login = $row['login']; ?> <table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr> <td colspan="2"><h3>Test Results Settings</h3></td> <td width="25%"> </td> <td width="25%"><?php echo $_SESSION['SESS_MEMBER_ID']; ?></td> </tr> <tr> <td colspan="4"><em>To turn off individual results gloablly across the site please use the following sliders (all results already added will also be made invisable)<br /> <br /> </em></td> </tr> <tr> <td width="25%"><div align='right'><strong><em>Salinity:</em></strong></div></td> <td width="25%"><input type="checkbox" value="test1" name="checks[]2" <?php echo (in_array("test1",$arr) ? "checked" : ''); ?>/></td> <td><div align='right'><strong><em>Phosphate:</em></strong></div></td> <td><input type="checkbox" value="test9" name="checks[]10" <?php echo (in_array("test9",$arr) ? "checked" : ''); ?>/></td> </tr> <tr> <td><div align='right'><strong><em>PH:</em></strong></div></td> <td><input type="checkbox" value="test2" name="checks[]3" <?php echo (in_array("test2",$arr) ? "checked" : ''); ?>/></td> <td><div align='right'><strong><em>potassium:</em></strong></div></td> <td><input type="checkbox" value="test10" name="checks[]11" <?php echo (in_array("test10",$arr) ? "checked" : ''); ?>/></td> </tr> <tr> <td><div align='right'><strong><em>Ammonia:</em></strong></div></td> <td><input type="checkbox" value="test3" name="checks[]4" <?php echo (in_array("test3",$arr) ? "checked" : ''); ?>/></td> <td><div align='right'><strong><em>Iodine:</em></strong></div></td> <td><input type="checkbox" value="test11" name="checks[]12" <?php echo (in_array("test11",$arr) ? "checked" : ''); ?>/></td> </tr> <tr> <td><div align='right'><strong><em>Nitrite:</em></strong></div></td> <td><input type="checkbox" value="test4" name="checks[]5" <?php echo (in_array("test4",$arr) ? "checked" : ''); ?>/></td> <td><div align='right'><strong><em>Strontium:</em></strong></div></td> <td><input type="checkbox" value="test12" name="checks[]13" <?php echo (in_array("test12",$arr) ? "checked" : ''); ?>/></td> </tr> <tr> <td><div align='right'><strong><em>Nitrate:</em></strong></div></td> <td><input type="checkbox" value="test5" name="checks[]6" <?php echo (in_array("test5",$arr) ? "checked" : ''); ?>/></td> <td><div align='right'><strong><em>Silica:</em></strong></div></td> <td><input type="checkbox" value="test13" name="checks[]14" <?php echo (in_array("test13",$arr) ? "checked" : ''); ?>/></td> </tr> <tr> <td><div align='right'><strong><em>Calcium:</em></strong></div></td> <td><input type="checkbox" value="test6" name="checks[]7" <?php echo (in_array("test6",$arr) ? "checked" : ''); ?>/></td> <td><div align='right'><strong><em>Temperatu </em></strong></div></td> <td><input type="checkbox" value="test14" name="checks[]" <?php echo (in_array("test14",$arr) ? "checked" : ''); ?>/></td> </tr> <tr> <td><div align='right'><strong><em>Magnesium:</em></strong></div></td> <td><input type="checkbox" value="test7" name="checks[]8" <?php echo (in_array("test7",$arr) ? "checked" : ''); ?>/></td> <td></td> <td></td> </tr> <tr> <td><div align='right'><strong><em>Alkalinity:</em></strong></div></td> <td><input type="checkbox" value="test8" name="checks[]9" <?php echo (in_array("test8",$arr) ? "checked" : ''); ?>/></td> <td colspan="2" align="right"></td> </tr> <tr> <td colspan="4" align="right"><table border="0" cellspacing="0" cellpadding="8"> <tr> <td><input class="Button" type="submit" name="submit" value="Submit" /></td> <td><input class="Button-alt" type="reset" name="reset" value="Reset" /></td> </tr> </table></td> </tr> </table> <br /> </form><?php } ?> </td> </tr> </table> </p> </div> This pulls data in array from database for example: test1;test9;test2;test3;test4;test5;test6;test14;test7;test8 The SELECT query works fine if i manually edit the array in the database to make the boxes checked or not. Problem seems to be when i click submit, its not updating the DATABASE?! i recently upgraded to PHP5 eventually and it seems it stopped working arond then. any help please. Code: [Select] //Update the ranks if (isset($_POST['update'])){ $ids = implode(",", array_map('intval', $_POST['m'])); $ranks = implode(",", array_map('intval', $_POST['ranks'])); if ($ids < 1) message("Incorrect Data"); if ($ranks < 0) message("Incorrect Data"); foreach ($_POST['ranks'] as $muffins){ $db->query('UPDATE friends set rank = '.$muffins.' WHERE friend_id = '.$_POST['m'].' AND user_id = '.$pun_user['id'].'') or error('Unable to remove users from online list', __FILE__, __LINE__, $db->error()); } redirect("s.php?section=Friends","Thanks, Ranks Updated"); } friend_id = becomes blank? the $muffins work and each rank is showed respectivaly, but how can I join in another array in the loop? I tried another foreach in the loop, but it just made me run double the queries. Any idea? $muffins is working fine and $_POST['m'] is giving no results i need to have 2 dynamic arrays in that forloop, the $ranks and the $ids |