-
Posts
124 -
Joined
-
Last visited
-
Days Won
1
Posts posted by happypete
-
-
I have a table with an ID column which is the Primary key and auto incremented. I have another column named ORDER that contains a number. This number is used to define the order in which the row data is displayed on a page.
When I delete a row, the ORDER column will have a gap and I would like to renumber the subsequent rows to remove the gap. ie:
I want to renumber the rows:
ID ORDER
1 1
2 2
4 3
5 4
When I insert a new row I want the ORDER row to be given the NEXT sequential number
How do I do that using PHP PDO?
Thanks
-
sorted it
Here is the working code:
<input type="text" name="dates[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['dates']; ?>" class="rates" /> <input type="text" name="night[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['night']; ?>" class="rates" /> <input type="text" name="week[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['week']; ?>" class="rates" /> <input type="text" name="month[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['month']; ?>" class="rates" /> <input type="text" name="min[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['min']; ?>" class="rates" /> <input type="text" name="rank[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['rank']; ?>" class="rates" />
$sql3 = "UPDATE ratestable SET dates=?, night=?, week=?, month=?, min=?, rank=? WHERE id=?"; $stmt3 = $db->prepare($sql3); if(count($_POST['rank']) > 0) { foreach($_POST['rank'] AS $key => $val) { $stmt3->execute(array( $_POST['dates'][$key], $_POST['night'][$key], $_POST['week'][$key], $_POST['month'][$key], $_POST['min'][$key], $val, $key )); }} $stmt3->closeCursor();
-
sorted it
Here is the working code:
<input type="text" name="dates[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['dates']; ?>" class="rates" /> <input type="text" name="night[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['night']; ?>" class="rates" /> <input type="text" name="week[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['week']; ?>" class="rates" /> <input type="text" name="month[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['month']; ?>" class="rates" /> <input type="text" name="min[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['min']; ?>" class="rates" /> <input type="text" name="rank[<?php echo $e2['id']; ?>]" maxlength="20" value="<?php echo $e2['rank']; ?>" class="rates" />
$sql3 = "UPDATE ratestable SET dates=?, night=?, week=?, month=?, min=?, rank=? WHERE id=?"; $stmt3 = $db->prepare($sql3); if(count($_POST['rank']) > 0) { foreach($_POST['rank'] AS $key => $val) { $stmt3->execute(array( $_POST['dates'][$key], $_POST['night'][$key], $_POST['week'][$key], $_POST['month'][$key], $_POST['min'][$key], $val, $key )); }} $stmt3->closeCursor();
-
updated the code to this:
$sql2 = "SELECT id, 1, 2, 3, 4, 5, 6 FROM ratestable ORDER BY id ASC"; $stmt2 = $db->prepare($sql2); $stmt2->execute(); $e2 = $stmt->fetch();
<?php while($e2 = $stmt2->fetch()) { ?> <input type="hidden" name="id" value="<?php echo $e2['id']; ?>" /> <input type="text" name="1" maxlength="10" value="<?php echo $e2['1'] ?>" class="rates" /> <input type="text" name="2" maxlength="10" value="<?php echo $e2['2'] ?>" class="rates" /> <input type="text" name="3" maxlength="10" value="<?php echo $e2['3'] ?>" class="rates" /> <input type="text" name="4" maxlength="10" value="<?php echo $e2['4'] ?>" class="rates" /> <input type="text" name="5" maxlength="10" value="<?php echo $e2['5'] ?>" class="rates" /> <input type="text" name="6" maxlength="10" value="<?php echo $e2['6'] ?>" class="rates" /> <?php } ?>
Can get it to update the last last ID but not all rows:
$sql = "UPDATE ratestable SET 1=?, 2=?, 3=?, 4=?, 5=?, 6=? WHERE id=?"; $stmt = $db->prepare($sql); $stmt->execute(array( $_POST['1'], $_POST['2'], $_POST['3'], $_POST['4'], $_POST['5'], $_POST['6'], $_POST['id'], )); $stmt->closeCursor();
-
You would have to put the results in a form and submit any changes to update. Here is a decent tutorial on that http://www.freewebmasterhelp.com/tutorials/phpmysql/7.
Example:
Step 1: Retrieve Data into form for user to edit with value attribute set with initial value
Step 2: Submit form with any changes
Step 3: Write code to update those changes in the db
I hope this helps!
1: So yes my results are in a form as per the code above....
2: Yes sent it to this page: rates-editupdate.php ( I know I got this bit right because I have simple update code written on my other pages of the site)
3: 'Write code to update those changes' This is where I need help.. I don't know how to write the code to update the database for each for the ID's.. If it was easy to find I would have found it by now as I have been searching the web since yesterday, hence the fact I'm asking for help on this forum...
The link sent me to a simple 'update a database' and a loop, so I look at the loop and cant figure how to write the code based on my example:
so somewhere in the code below I will need a loop and a count????
$sql = "UPDATE ratestable SET 1=?, 2=?, 3=?, 4=?, 5=?, 6=? WHERE id=1"; $stmt = $db->prepare($sql); foreach($stmt as $key) { $stmt->execute(array( $_POST['1'], $_POST['2'], $_POST['3'], $_POST['4'], $_POST['5'], $_POST['6'], ) ); } $stmt->closeCursor();
-
Thanks, but that link didn't help at all. I can update an entry in a database if it was a single entry it would be something like this:
$sql = "UPDATE ratestable SET 1=?, 2=?, 3=?, 4=?, 5=?, 6=? WHERE id=1"; $stmt = $db->prepare($sql); foreach($stmt as $key) { $stmt->execute(array( $_POST['1'], $_POST['2'], $_POST['3'], $_POST['4'], $_POST['5'], $_POST['6'], ) ); } $stmt->closeCursor();
but what I can't do is update multiple entries of the same table at the same time...
This code below prints out the data for EACH ID in the 'ratestable' and makes it editable, so I need to be able to update multiple rows at a time, that is what I'm having the trouble with... so I'm thinking it's going to have a loop, foreach or array or something.....??????
<?php while($e2 = $stmt2->fetch()) { ?> <input type="text" name="1" maxlength="10" value="<?php echo $e2['1'] ?>" class="rates" /> <input type="text" name="2" maxlength="10" value="<?php echo $e2['2'] ?>" class="rates" /> <input type="text" name="3" maxlength="10" value="<?php echo $e2['3'] ?>" class="rates" /> <input type="text" name="4" maxlength="10" value="<?php echo $e2['4'] ?>" class="rates" /> <input type="text" name="5" maxlength="10" value="<?php echo $e2['5'] ?>" class="rates" /> <input type="text" name="6" maxlength="10" value="<?php echo $e2['6'] ?>" class="rates" /> <?php } ?>
-
I can get the info from the database fine with this:
$sql2 = "SELECT 1, 2, 3, 4, 5, 6 FROM ratestable ORDER BY id ASC"; $stmt2 = $db->prepare($sql2); $stmt2->execute(); $e2 = $stmt->fetch();
and display it with this:
<?php while($e2 = $stmt2->fetch()) { ?> <input type="text" name="1" maxlength="10" value="<?php echo $e2['1'] ?>" class="rates" /> <input type="text" name="2" maxlength="10" value="<?php echo $e2['2'] ?>" class="rates" /> <input type="text" name="3" maxlength="10" value="<?php echo $e2['3'] ?>" class="rates" /> <input type="text" name="4" maxlength="10" value="<?php echo $e2['4'] ?>" class="rates" /> <input type="text" name="5" maxlength="10" value="<?php echo $e2['5'] ?>" class="rates" /> <input type="text" name="6" maxlength="10" value="<?php echo $e2['6'] ?>" class="rates" /> <?php } ?>
How do I update it to the database?
$sql = "UPDATE rates SET title1=?, title2=?, title3=?, title4=?, title5=?, title6=? THE CODE HERE IS WHERE I AM STUCK....
and that's as far as I can get, need to use an array or foreach???????
-
Thanks for the responses, but Ill try and explain myself better.
I can get the info from the database fine with this:
$sql2 = "SELECT 1, 2, 3, 4, 5, 6 FROM ratestable ORDER BY id ASC"; $stmt2 = $db->prepare($sql2); $stmt2->execute(); $e2 = $stmt->fetch();
and display it with this:
<?php while($e2 = $stmt2->fetch()) { ?> <input type="text" name="1" maxlength="10" value="<?php echo $e2['1'] ?>" class="rates" /> <input type="text" name="2" maxlength="10" value="<?php echo $e2['2'] ?>" class="rates" /> <input type="text" name="3" maxlength="10" value="<?php echo $e2['3'] ?>" class="rates" /> <input type="text" name="4" maxlength="10" value="<?php echo $e2['4'] ?>" class="rates" /> <input type="text" name="5" maxlength="10" value="<?php echo $e2['5'] ?>" class="rates" /> <input type="text" name="6" maxlength="10" value="<?php echo $e2['6'] ?>" class="rates" /> <?php } ?>
How do I update it to the database?
$sql = "UPDATE rates SET title1=?, title2=?, title3=?, title4=?, title5=?, title6=? THE CODE HERE IS WHERE I AM STUCK....
and that's as far as I can get, need to use an array or foreach???????
-
Hi,
I'm a beginner at PHP and am I'm trying to update an array but cant get it to work:
This is my 'rates' page, There are 6 boxes that are the headings, and 6 boxes per line below for the data, I can get the headings to save but not the data lines below:
rates-edit.php
<?php error_reporting(E_ALL); ini_set('display_errors', 2); // Include the necessary files include_once '../inc/db.inc.php'; // Open a database connection $db = new PDO(DB_INFO, DB_USER, DB_PASS); // Extract details from database $sql = "SELECT title1, title2, title3, title4, title5, title6 FROM rates WHERE id=1"; $stmt = $db->prepare($sql); $stmt->execute(); $e = $stmt->fetch(); $sql2 = "SELECT 1, 2, 3, 4, 5, 6 FROM ratestable ORDER BY id ASC"; $stmt2 = $db->prepare($sql2); $stmt2->execute(); $e2 = $stmt->fetch(); ?> <!DOCTYPE html> <html lang="en"> <head> <h3>Rates Table</h3> <form method="post" action="rates-editupdate.php" enctype="multipart/form-data"> <input type="text" name="title1" maxlength="10" value="<?php echo $e['title1'] ?>" class="rates" /> <input type="text" name="title2" maxlength="10" value="<?php echo $e['title1'] ?>" class="rates" /> <input type="text" name="title3" maxlength="10" value="<?php echo $e['title3'] ?>" class="rates" /> <input type="text" name="title4" maxlength="10" value="<?php echo $e['title4'] ?>" class="rates" /> <input type="text" name="title5" maxlength="10" value="<?php echo $e['title5'] ?>" class="rates" /> <input type="text" name="title6" maxlength="10" value="<?php echo $e['title6'] ?>" class="rates" /> <?php while($e2 = $stmt2->fetch()) { ?> <input type="text" name="1" maxlength="10" value="<?php echo $e2['1'] ?>" class="rates" /> <input type="text" name="2" maxlength="10" value="<?php echo $e2['2'] ?>" class="rates" /> <input type="text" name="3" maxlength="10" value="<?php echo $e2['3'] ?>" class="rates" /> <input type="text" name="4" maxlength="10" value="<?php echo $e2['4'] ?>" class="rates" /> <input type="text" name="5" maxlength="10" value="<?php echo $e2['5'] ?>" class="rates" /> <input type="text" name="6" maxlength="10" value="<?php echo $e2['6'] ?>" class="rates" /> <?php } ?> <input id="button-upload" class="button" type="submit" name="submit" value="Save Changes" /> <input id="button-upload" class="button" type="submit" name="submit" value="Add New Row" /> </form> </body> </html>
This is the page that updates the database, I cant get the loop/foreach to work...
rates-editupdate.php
<?php error_reporting(E_ALL); ini_set('display_errors', 2); // Include the necessary files include_once '../inc/db.inc.php'; // Open a database connection $db = new PDO(DB_INFO, DB_USER, DB_PASS); // Check if coming from a POST command and Save Changes // THIS BIT WORKS if($_SERVER['REQUEST_METHOD']=='POST' && $_POST['submit']=='Save Changes') { $sql = "UPDATE rates SET title1=?, title2=?, title3=?, title4=?, title5=?, title6=? WHERE id=1 LIMIT 1"; $stmt = $db->prepare($sql); $stmt->execute( array( $_POST['title1'], $_POST['title2'], $_POST['title3'], $_POST['title4'], $_POST['title5'], $_POST['title6'] ) ); $stmt->closeCursor(); // // THIS IS THE BIT THAT DOES NOT UPDATE: // $sql = "UPDATE ratestable SET 1=?, 2=?, 3=?, 4=?, 5=?, 6=? WHERE id=?"; $stmt = $db->prepare($sql); if(count($_POST['1']) > 0) { foreach($_POST AS $key => $val) { $stmt->execute(array( ($key), ($_POST['2'][$key]), ($_POST['3'][$key]), ($_POST['4'][$key]), ($_POST['5'][$key]), ($_POST['6'][$key]), ($val) ) ); } $stmt->closeCursor(); } // once updated return to rates page header('Location: rates-edit.php?success=1'); exit; } ?>
thanks in advance
-
Hi,
I've been looking for a very simple contact form the validates server side - email and name field required. I will only recieve about 20 emails a month so it doent need to be complicated.
I have compiled a script from diffrent code I have found online.
The spam trap 'url' works, the validation works, but when it sends it say 'message sent' but the mail never arrives. It did arrive before I put the 'URL' field in.
When I took the 'echo "message sent' out and used the 'header("thanks.php");' it didnt redirect either.
Can anyone help?
Thanks in advance.
<?php function validateName($name){ //if it's NOT valid if(strlen($name) < 4) return false; //if it's valid else return true; } function validateEmail($email){ return ereg("^[a-zA-Z0-9]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$", $email); } ?> <!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" dir="ltr"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>form</title> <link rel="stylesheet" href="css/general.css" type="text/css" media="screen" /> </head> <body> <?php if( isset($_POST['send']) && (!validateName($_POST['name']) || !validateEmail($_POST['email']) ) ):?> <p> <?php if(!validateName($_POST['name'])): ?> <li><strong>Invalid Name:</strong>Names with more than 3 letters please</li> <?php endif ?> <?php if(!validateEmail($_POST['email'])): ?> <li><strong>Invalid E-mail:</strong>Invalid Email</li> <?php endif ?> </p> <?php elseif(isset($_POST['send'])): if(!empty($_POST['URL'])){ die('Have a nice day elsewhere we think you are SPAM - If please try again.'); } else { // Configuration Settings $SendFrom = "NO REPLY <name@removed.com>"; $SendTo = "name <name@removed.com>"; $SubjectLine = "subject"; // Build Message Body from Web Form Input foreach ($_POST as $Field=>$Value) $MsgBody .= "$Field: $Value\n"; $MsgBody .= "\n" . @gethostbyaddr($_SERVER["REMOTE_ADDR"]) . "\n" . $_SERVER["HTTP_USER_AGENT"]; $MsgBody = htmlspecialchars($MsgBody, ENT_NOQUOTES); //make content safe // Send E-Mail and Direct Browser to Confirmation Page mail($SendTo, $SubjectLine, $MsgBody, "From: $SendFrom"); echo "message sent"; //header("thanks.php"); exit; } endif ?> <form method="post" id="customForm" action=""> <div> <input id="URL" name="URL" type="text" value="<?php echo $_POST["URL"]; ?>" /> </div> <div> <label for="name">Name</label> <input id="name" name="name" type="text" value="<?php echo $_POST["name"]; ?>" /> <span id="nameInfo">What's your name?</span> </div> <div> <label for="email">E-mail</label> <input id="email" name="email" type="text" value="<?php echo $_POST["email"]; ?>" /> <span id="emailInfo">Valid E-mail please, you will need it to log in!</span> </div> <div> <label for="subject">Phone</label> <input id="subject" name="subject" type="text" value="<?php echo $_POST["phone"]; ?>" /> <span id="pass1Info">At least 5 characters: letters, numbers and '_'</span> </div> <div> <label for="check">Check</label> <input id="check" name="check" type="text" value="<?php echo $_POST["check"]; ?>"/> <span id="pass2Info">Confirm password</span> </div> <div> <label for="message">Message</label> <textarea id="message" name="message" cols="10" rows="4" value="<?php echo $_POST["message"]; ?>"></textarea> </div> <div> <input id="send" name="send" type="submit" value="send" /> </div> </form> </div> </body> </html>
-
thanks thorpe
-
Hi,
I'm designing a site that contains mini-sites on sub-domains: 1.site.com, 2.site.com, 3.site.com etc...
Each site will use the following script to extract the sub-domain:
<?php $subdir = basename(dirname(__FILE__)); echo "$subdir"; ?>
and everything will revolve around this number, so for www.1.site.com $subdir equals 1
I will then use this number to relate to the table 'id' within the database to extract data:
$sql = "SELECT title FROM data WHERE id=$subdir";
so 1.site.com will call data from the database with id=1
I then use an addon domain on the sub-domain ie: thisdomain.com = 1.site.com, so thisdomain.com will extract data with id=1 from the database.
so my question, will this work and will it be reliable?
-
so if I want to host another domain on the server does it have to be a subdomain?
-
http://themeshift.com they have property themes
-
Hi,
I'm working on a site: you sign up and it creates your own mini-site which has a separate domain pointed to it (so it functions as an individual site).
where should this mini-site be located?
1) a directory
2) a subdomain
3) with url redirection, .htaccss?
is there a difference in terms of SEO, load on the server etc...
thanks in advance
-
Hi,
I can 'echo' the array, but can't get it to update, not sure about the code:
This prints the array:
<?php error_reporting(E_ALL); ini_set('display_errors', 2); // Include the necessary files include_once 'inc/db.inc.php'; // Open a database connection $db = new PDO(DB_INFO, DB_USER, DB_PASS); // Extract details from database $sql2 = "SELECT id, dates, night, week, month, min, rank FROM rates ORDER BY rank ASC"; $stmt2 = $db->prepare($sql2); $stmt2->execute(); $e2 = $stmt2->fetch(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <link rel="stylesheet" href="css/default.css" type="text/css" /> <title>Test Site</title> </head> <body> <h1>Test Site</h1> <?php include('menu.php'); ?> <form method="post" action="arraytestupdate.php" enctype="multipart/form-data"> <fieldset> <label>Rates<div class="clear"></div> <?php foreach($stmt2 as $e2) { ?> <input type="hidden" name="id" value="id=<?php echo $e2['id']; ?>" /> <input class="mediuminput" type="text" name="dates" value="<?php echo $e2['dates']; ?>" /> <input class="smallinput" type="text" name="night" value="<?php echo $e2['night']; ?>" /> <input class="smallinput" type="text" name="week" value="<?php echo $e2['week']; ?>" /> <input class="smallinput" type="text" name="month"value="<?php echo $e2['month']; ?>" /> <input class="smallinput" type="text" name="min" value="<?php echo $e2['min']; ?>" /> <input class="smallinput" type="text" name="rank" value="<?php echo $e2['rank']; ?>" /> </label> <?php } ?> <input class="green" type="submit" name="submit" value="Update Entry" /> </fieldset> </form> </body> </html>
Not sure about the correct code to update..i need a 'foreach' in there somewhere??
<?php // Include the necessary files include_once 'inc/db.inc.php'; // Open a database connection $db = new PDO(DB_INFO, DB_USER, DB_PASS); // Check if coming from a POST command and upadte the RATES if($_SERVER['REQUEST_METHOD']=='POST' && $_POST['submit']=='Update Entry') { $sql = "UPDATE rates SET dates=?, night=?, week=?, month=?, min=?, rank=? WHERE id=?"; $stmt = $db->prepare($sql); $stmt->execute( array( $_POST['dates'], $_POST['night'], $_POST['week'], $_POST['month'], $_POST['min'], $_POST['rank'], $_POST['id'], ) ); $stmt->closeCursor(); // once updated return to admin page header('Location: arraytest.php'); exit; } else { // If nothing happend send back to admin page header('Location: arraytest.php'); exit; } ?>
thanks in advance
-
Thank Maq, but could you be a bit more specific please, I don't understand you answer. thanks
-
Hi, I can do this with MySQl but am now trying with PDO but it wont update all the rows. I assume i need a 'foreach' somewhere but can't work it out. It only updates where id=3
HTML form and PHP update script included:
admin5.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <link rel="stylesheet" href="css/default.css" type="text/css" /> <title>Test Site</title> </head> <body> <h1>Test Site</h1> <form method="post" action="5update.php" enctype="multipart/form-data"> <fieldset> <label>Rates <div class="clear"></div> <input type="hidden" name="id" value="1" /> <input class="mediuminput" type="text" name="dates" value="ski seasons" /> <input class="smallinput" type="text" name="night" value="u$s 34" /> <input class="smallinput" type="text" name="week" value="u$s 345" /> <input class="smallinput" type="text" name="month"value="u$s 1234" /> <input class="smallinput" type="text" name="min" value="3 nights" /> <input class="smallinput" type="text" name="rank" value="2" /> </form> </label> <input type="hidden" name="id" value="2" /> <input class="mediuminput" type="text" name="dates" value="ewrew" /> <input class="smallinput" type="text" name="night" value="wer" /> <input class="smallinput" type="text" name="week" value="wer" /> <input class="smallinput" type="text" name="month"value="ewqr" /> <input class="smallinput" type="text" name="min" value="weqr" /> <input class="smallinput" type="text" name="rank" value="3" /> </form> </label> <input type="hidden" name="id" value="3" /> <input class="mediuminput" type="text" name="dates" value="123dds" /> <input class="smallinput" type="text" name="night" value="asw" /> <input class="smallinput" type="text" name="week" value="as" /> <input class="smallinput" type="text" name="month"value="as" /> <input class="smallinput" type="text" name="min" value="as" /> <input class="smallinput" type="text" name="rank" value="4" /> </form> </label> <div class="clear"></div> <input class="green" type="submit" name="submit" value="Update Entry" /> </fieldset> </form> </body> </html>
5update.php
<?php // Include the necessary files include_once 'inc/db.inc.php'; // Open a database connection $db = new PDO(DB_INFO, DB_USER, DB_PASS); // Check if coming from a POST command and if($_SERVER['REQUEST_METHOD']=='POST' && $_POST['submit']=='Update Entry') { // update data into the database if(!empty($_POST['id'])) //$id = $_POST['id']; $sql = "UPDATE rates SET dates=?, night=?, week=?, month=?, min=?, rank=? WHERE id=?"; $stmt = $db->prepare($sql); $stmt->execute( array( $_POST['dates'], $_POST['night'], $_POST['week'], $_POST['month'], $_POST['min'], $_POST['rank'], $_POST['id'], ) ); $stmt->closeCursor(); // once updated return to admin page header('Location: admin5.php'); exit; } else { // If nothing happend send back to admin page header('Location: admin5.php'); exit; } ?>
-
AWESOME
Thanks MadTechie, cags, PFMaBiSmAd it's finally working, I've spent over a week trying to get it to work....
Here is the final code:
<?php error_reporting(E_ALL); // It double checks to see if I'm logged in require_once 'sources/login/classes/Membership.php'; $membership = New Membership(); $membership->confirm_Member(); $db_name = "trek_trek"; $db_server = "localhost"; $db_user = "trek_user"; $db_pass = "intergreen"; $change = ""; $abc = ""; define("MAX_SIZE", "400"); $errors = 0; $rank = $_POST['rank']; $description = $_POST['description']; $id = $_POST['id']; // Check if button name "Submit" is active, do this include ('testconfig.php'); if (isset($_POST['Update'])) { foreach($_POST['description'] as $id => $description){ $rank = $_POST['rank'][$id]; $sql1 = "UPDATE photo SET rank='$rank', description='$description' WHERE id=$id"; $result1 = mysql_query($sql1) or die(mysql_error()); if ($result1) { header("location:index.php"); } } } if (isset($_POST['Upload'])) { $image = $_FILES["file"]["name"]; $uploadedfile = $_FILES['file']['tmp_name']; if ($image) { $filename = stripslashes($_FILES['file']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { $change = '<div class="msgdiv">Unknown Image extension </div> '; $errors = 1; } else { $size = filesize($_FILES['file']['tmp_name']); if ($size > MAX_SIZE * 1024) { $change = '<div class="msgdiv">You have exceeded the size limit!</div> '; $errors = 1; } if ($extension == "jpg" || $extension == "jpeg") { $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); } else if ($extension == "png") { $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefrompng($uploadedfile); } else { $src = imagecreatefromgif($uploadedfile); } list ($width, $height) = getimagesize($uploadedfile); $newwidth = 600; $newheight = ($height / $width) * $newwidth; $tmp = imagecreatetruecolor($newwidth, $newheight); $newwidth1 = 200; $newheight1 = ($height / $width) * $newwidth1; $tmp1 = imagecreatetruecolor($newwidth1, $newheight1); imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagecopyresampled($tmp1, $src, 0, 0, 0, 0, $newwidth1, $newheight1, $width, $height); $filename = "uploads/" . $_FILES['file']['name']; $filename1 = "uploads/tn/" . $_FILES['file']['name']; imagejpeg($tmp, $filename, 100); imagejpeg($tmp1, $filename1, 100); imagedestroy($src); imagedestroy($tmp); imagedestroy($tmp1); } } //If no errors registred, print the success message if ($errors == 0) { $q = "INSERT into photo(description, src, tn_src) VALUES('$description', '$filename', '$filename1')"; //$mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name) or die(mysqli_error()); //$result = $mysqli->query($q) or die(mysqli_error($mysqli)); $result = mysql_query($q) or die(mysql_error()); if ($result) { $change = '<div class="msgdiv">Image Uploaded Successfully!</div>'; } }} ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Admin Page</title> <link rel="stylesheet" href="sources/styles.css" type="text/css" /> <link rel="stylesheet" href="admin.css" type="text/css" /> </head> <body> <div id="container"> <div id="header"> <h1>Admin page</h1> </div> <div id="content"> <div id="page"> <?php echo $change; ?> <table width="502" cellpadding="0" cellspacing="0" id="main"> <tbody> <tr> <td width="500" height="238" valign="top" id="main_right"> <div id="posts"> <form method="post" action="" enctype="multipart/form-data" name="form1"> <table width="500" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <Td style="height: 25px"> </Td> </tr> <tr> <td width="150"> <div align="right" class="titles">Picture :</div> </td> <td width="350" align="left"> <div align="left"><input size="25" name="file" type="file" style="font-family: Verdana; font-size: 10pt" class="box" /></div> </td> </tr> <tr> <Td></Td> <Td valign="top" height="35px" class="help">Image maximum size <b>400 </b>kb</span></Td> </tr> <tr> <Td></Td> <Td valign="top" height="35px"><label for="description">Description</label><br /> <input type="text" name="description" value="" /><br /> </Td> </tr> <tr> <Td></Td> <Td valign="top" height="35px"><input type="submit" id="mybut" value="Upload" name="Upload" /></Td> </tr> <tr> <td width="200"> </td> <td width="200"> <table width="200" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="200" align="center"> <div align="left"></div> </td> <td width="100"> </td> </tr> </table> </td> </tr> </table> </form> </div> </td> </tr> </tbody> </table> <form method="POST" action=""> <?php include ('testconfig.php'); $result = mysql_query("SELECT * FROM photo ORDER BY rank ASC") or die("Bad query: " . mysql_error()); while ($row = mysql_fetch_array($result)) { ?> <input type="text" name="rank[<?php print $row['id']; ?>]" size="2" value="<?php print $row['rank']; ?>"> <input type="text" name="description[<?php print $row['id']; ?>]" size="" value="<?php print $row['description']; ?>"> <?php echo "<img src=\"" . $row['tn_src'] . "\"/>"; echo " <a href=delete1.php?id={$row['id']}>Delete</a><br>"; } // END WHILE ?> <input type="submit" value="Update" name="Update"> </form> </div>1.0 <?php function getExtension ($str) { $i = strrpos($str, "."); if (! $i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } ?>
-
awesome, nearly there
It uploads fine and displays the thumbnails and editable descriptions, but only the original description is editable/updatable, the others don't update, so I assume there is something that needs to be done to the bit of code below to make it update all entires: 'array' / 'for each' ???
if (isset($_POST['Update'])) { $sql1 = "UPDATE photo SET rank='$rank', description='$description' WHERE id=$id"; $result1 = mysql_query($sql1) or die(mysql_error()); if ($result1) { header("location:index.php"); } }
all the code:
<?php error_reporting(E_ALL); // It double checks to see if I'm logged in require_once 'sources/login/classes/Membership.php'; $membership = New Membership(); $membership->confirm_Member(); $db_name = "trek_trek"; $db_server = "localhost"; $db_user = "trek_user"; $db_pass = "intergreen"; $change = ""; $abc = ""; define("MAX_SIZE", "400"); $errors = 0; $rank = $_POST['rank']; $description = $_POST['description']; $id = $_POST['id']; // Check if button name "Submit" is active, do this include ('testconfig.php'); if (isset($_POST['Update'])) { $sql1 = "UPDATE photo SET rank='$rank', description='$description' WHERE id=$id"; $result1 = mysql_query($sql1) or die(mysql_error()); if ($result1) { header("location:index.php"); } } if (isset($_POST['Upload'])) { $image = $_FILES["file"]["name"]; $uploadedfile = $_FILES['file']['tmp_name']; if ($image) { $filename = stripslashes($_FILES['file']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { $change = '<div class="msgdiv">Unknown Image extension </div> '; $errors = 1; } else { $size = filesize($_FILES['file']['tmp_name']); if ($size > MAX_SIZE * 1024) { $change = '<div class="msgdiv">You have exceeded the size limit!</div> '; $errors = 1; } if ($extension == "jpg" || $extension == "jpeg") { $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); } else if ($extension == "png") { $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefrompng($uploadedfile); } else { $src = imagecreatefromgif($uploadedfile); } list ($width, $height) = getimagesize($uploadedfile); $newwidth = 600; $newheight = ($height / $width) * $newwidth; $tmp = imagecreatetruecolor($newwidth, $newheight); $newwidth1 = 200; $newheight1 = ($height / $width) * $newwidth1; $tmp1 = imagecreatetruecolor($newwidth1, $newheight1); imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagecopyresampled($tmp1, $src, 0, 0, 0, 0, $newwidth1, $newheight1, $width, $height); $filename = "uploads/" . $_FILES['file']['name']; $filename1 = "uploads/tn/" . $_FILES['file']['name']; imagejpeg($tmp, $filename, 100); imagejpeg($tmp1, $filename1, 100); imagedestroy($src); imagedestroy($tmp); imagedestroy($tmp1); } } //If no errors registred, print the success message if ($errors == 0) { $q = "INSERT into photo(description, src, tn_src) VALUES('$description', '$filename', '$filename1')"; //$mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name) or die(mysqli_error()); //$result = $mysqli->query($q) or die(mysqli_error($mysqli)); $result = mysql_query($q) or die(mysql_error()); if ($result) { $change = '<div class="msgdiv">Image Uploaded Successfully!</div>'; } }} ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Admin Page</title> <link rel="stylesheet" href="sources/styles.css" type="text/css" /> <link rel="stylesheet" href="admin.css" type="text/css" /> </head> <body> <div id="container"> <div id="header"> <h1>Admin page</h1> </div> <div id="content"> <div id="page"> <?php echo $change; ?> <table width="502" cellpadding="0" cellspacing="0" id="main"> <tbody> <tr> <td width="500" height="238" valign="top" id="main_right"> <div id="posts"> <form method="post" action="" enctype="multipart/form-data" name="form1"> <table width="500" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <Td style="height: 25px"> </Td> </tr> <tr> <td width="150"> <div align="right" class="titles">Picture :</div> </td> <td width="350" align="left"> <div align="left"><input size="25" name="file" type="file" style="font-family: Verdana; font-size: 10pt" class="box" /></div> </td> </tr> <tr> <Td></Td> <Td valign="top" height="35px" class="help">Image maximum size <b>400 </b>kb</span></Td> </tr> <tr> <Td></Td> <Td valign="top" height="35px"><label for="description">Description</label><br /> <input type="text" name="description" value="" /><br /> </Td> </tr> <tr> <Td></Td> <Td valign="top" height="35px"><input type="submit" id="mybut" value="Upload" name="Upload" /></Td> </tr> <tr> <td width="200"> </td> <td width="200"> <table width="200" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="200" align="center"> <div align="left"></div> </td> <td width="100"> </td> </tr> </table> </td> </tr> </table> </form> </div> </td> </tr> </tbody> </table> <form method="POST" action=""> <?php include ('testconfig.php'); $result = mysql_query("SELECT * FROM photo ORDER BY rank ASC") or die("Bad query: " . mysql_error()); while ($row = mysql_fetch_array($result)) { ?> <input type="text" name="id" size="2" value="<?php print $row['id']; ?>"> <input type="text" name="rank" size="2" value="<?php print $row['rank']; ?>"> <input type="text" name="description" size="" value="<?php print $row['description']; ?>"> <?php echo "<img src=\"" . $row['tn_src'] . "\"/>"; echo " <a href=delete1.php?id={$row['id']}>Delete</a><br>"; } // END WHILE ?> <input type="submit" value="Update" name="Update"> </form> </div>1.0 <?php function getExtension ($str) { $i = strrpos($str, "."); if (! $i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } ?>
-
I've already put in 'include ('testconfig.php');' and the post above shows the results with it added
// Check if button name "Submit" is active, do this include ('testconfig.php'); if (isset($_POST['Update'])) { $sql1 = "UPDATE photo SET rank='$rank', description='$description' WHERE id=$id"; $result1 = mysql_query($sql1) or die(mysql_error()); if ($result1) { header("location:index1.php"); } }
full code:
<?php error_reporting(E_ALL); // It double checks to see if I'm logged in require_once 'sources/login/classes/Membership.php'; $membership = New Membership(); $membership->confirm_Member(); $db_name = "trek_trek"; $db_server = "localhost"; $db_user = "trek_user"; $db_pass = "intergreen"; $change = ""; $abc = ""; define("MAX_SIZE", "400"); $errors = 0; $rank = $_POST['rank']; $description = $_POST['description']; $id = $_POST['id']; // Check if button name "Submit" is active, do this include ('testconfig.php'); if (isset($_POST['Update'])) { $sql1 = "UPDATE photo SET rank='$rank', description='$description' WHERE id=$id"; $result1 = mysql_query($sql1) or die(mysql_error()); if ($result1) { header("location:index1.php"); } } if (isset($_POST['Upload'])) { $image = $_FILES["file"]["name"]; $uploadedfile = $_FILES['file']['tmp_name']; if ($image) { $filename = stripslashes($_FILES['file']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { $change = '<div class="msgdiv">Unknown Image extension </div> '; $errors = 1; } else { $size = filesize($_FILES['file']['tmp_name']); if ($size > MAX_SIZE * 1024) { $change = '<div class="msgdiv">You have exceeded the size limit!</div> '; $errors = 1; } if ($extension == "jpg" || $extension == "jpeg") { $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); } else if ($extension == "png") { $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefrompng($uploadedfile); } else { $src = imagecreatefromgif($uploadedfile); } list ($width, $height) = getimagesize($uploadedfile); $newwidth = 600; $newheight = ($height / $width) * $newwidth; $tmp = imagecreatetruecolor($newwidth, $newheight); $newwidth1 = 200; $newheight1 = ($height / $width) * $newwidth1; $tmp1 = imagecreatetruecolor($newwidth1, $newheight1); imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagecopyresampled($tmp1, $src, 0, 0, 0, 0, $newwidth1, $newheight1, $width, $height); $filename = "uploads/" . $_FILES['file']['name']; $filename1 = "uploads/tn/" . $_FILES['file']['name']; imagejpeg($tmp, $filename, 100); imagejpeg($tmp1, $filename1, 100); imagedestroy($src); imagedestroy($tmp); imagedestroy($tmp1); } } } //If no errors registred, print the success message if ($errors == 0) { $q = "INSERT into photo(description, src, tn_src) VALUES('$description', '$filename', '$filename1')"; //$mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name) or die(mysqli_error()); //$result = $mysqli->query($q) or die(mysqli_error($mysqli)); $result = mysql_query($q) or die(mysql_error()); if ($result) { $change = '<div class="msgdiv">Image Uploaded Successfully!</div>'; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Admin Page</title> <link rel="stylesheet" href="sources/styles.css" type="text/css" /> <link rel="stylesheet" href="admin.css" type="text/css" /> </head> <body> <div id="container"> <div id="header"> <h1>Admin page</h1> </div> <div id="content"> <div id="page"> <?php echo $change; ?> <table width="502" cellpadding="0" cellspacing="0" id="main"> <tbody> <tr> <td width="500" height="238" valign="top" id="main_right"> <div id="posts"> <form method="post" action="" enctype="multipart/form-data" name="form1"> <table width="500" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <Td style="height: 25px"> </Td> </tr> <tr> <td width="150"> <div align="right" class="titles">Picture :</div> </td> <td width="350" align="left"> <div align="left"><input size="25" name="file" type="file" style="font-family: Verdana; font-size: 10pt" class="box" /></div> </td> </tr> <tr> <Td></Td> <Td valign="top" height="35px" class="help">Image maximum size <b>400 </b>kb</span></Td> </tr> <tr> <Td></Td> <Td valign="top" height="35px"><label for="description">Description</label><br /> <input type="text" name="description" value="" /><br /> </Td> </tr> <tr> <Td></Td> <Td valign="top" height="35px"><input type="submit" id="mybut" value="Upload" name="Upload" /></Td> </tr> <tr> <td width="200"> </td> <td width="200"> <table width="200" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="200" align="center"> <div align="left"></div> </td> <td width="100"> </td> </tr> </table> </td> </tr> </table> </form> </div> </td> </tr> </tbody> </table> <form method="POST" action=""> <?php include ('testconfig.php'); $result = mysql_query("SELECT * FROM photo ORDER BY rank ASC") or die("Bad query: " . mysql_error()); while ($row = mysql_fetch_array($result)) { ?> <input type="text" name="id" size="2" value="<?php print $row['id']; ?>"> <input type="text" name="rank" size="2" value="<?php print $row['rank']; ?>"> <input type="text" name="description" size="" value="<?php print $row['description']; ?>"> <?php echo "<img src=\"" . $row['tn_src'] . "\"/>"; echo " <a href=delete1.php?id={$row['id']}>Delete</a><br>"; } // END WHILE ?> <input type="submit" value="Update" name="Update"> </form> </div>1.0 <?php function getExtension ($str) { $i = strrpos($str, "."); if (! $i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } ?>
-
thanks for the updating the script. Now each time I try and update or refresh the page it adds a new input box (see attached screenshot), but doesn't update, although it is now getting redirected to index1.php as per:
if ($result1) { header("location:index1.php");
-
now going backwards, my update part still doesn't update and now the upload script doesn't work any more
-
cut and pasted that...but still not working....
Renumber row in sequential order using PDO
in PHP Coding Help
Posted
Thanks, but I'm not sure how to implement them, I tried this but it doen't work.. (RANK is the ORDER row)