Jump to content

bschultz

Members
  • Posts

    486
  • Joined

  • Last visited

Everything posted by bschultz

  1. I tried that, but it still didn't work. By the way, why wouldn't $cmd be an array? If there are 20 records matching today's date, I'll need $cmd[2] to delete the second row...otherwise, it deletes everything, wouldn't it?
  2. That's why I said that it doesn't appear to be doing anything on the delete if statement. I'm starting to think I have a logic problem here anyway. This script will display EVERY record in the database matching today's date. If the code I posted actually worked, it would stop at the first delete statement. I want to be able to echo ALL records into the html form, edit ALL records that need to be edited, and delete all the records that need to be deleted ALL AT ONCE. The way that I wrote this code, that won't happen. Any ideas on how to lay out this code to accomplish what I want?
  3. No error. This is the result echoed by this line: if ($dosql){ echo $usql; UPDATE cancellations SET date='2007-03-24', type='business', event='bagley library', action='Closed', comments='' WHERE row_number='17' So, it appears that the code isn't doing anything for the delete if statement.
  4. I'm having a problem getting the proper syntax to get this working. I want to display (in an html form) the results currently in the database. I then want to be able to update the records. This part of the script is working. Now, I'm trying to add a "delete row" clause within the update script. Here's what I have so far: <? putenv("TZ=US/Central"); $DBhost = "localhost"; $DBuser = "username"; $DBpass = "password"; $DBName = "cancellations"; mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database"); @mysql_select_db("$DBName") or die("Unable to select database $DBName"); $date = $_POST[date]; $type = $_POST[type]; $event = $_POST[event]; $action = $_POST[action]; $comments = $_POST[comments]; $row_number = $_POST[row_number]; $cmd = $_POST[cmd]; for ($i=0;$i<count($_POST[event]);$i++){ if('$cmd[$i]'=="delete") { $sqldelete = "DELETE FROM cancellations WHERE row_number='$row_number[$i]'"; $resultdelete = mysql_query($sqldelete); echo $sqldelete; // echo "Row deleted!"; } else $usql = "UPDATE cancellations SET date='$date[$i]', type='$type[$i]', event='$event[$i]', action='$action[$i]', comments='$comments[$i]' WHERE row_number='$row_number[$i]'"; // above compiles query $dosql = mysql_query($usql); // executes query } if ($dosql){ echo $usql; } else{ echo mysql_errno().": ".mysql_error()."<BR>"; } mysql_close (); ?> The delete clause isn't doing anything...just the update clause is working. Any ideas? thanks for your help. Brian
  5. Thank you! Call me stupid! What works for ONE statement, doesn't always work for TWO!
  6. I'm trying to implement a weather cancellation script. A user can enter in a cancellation (school, business, meeting, sports event) because of the weather, and it will display on a main page. The script is working to a certain extent. There are several categories that I'm displaying on the page. As long as there are records that match, it works just fine. If one select statement returns "false" it halts the remainder of the script. So, in the code below, if the first select returns false, the second statement won't run. Can someone tell me where my code is wrong? Thanks. <p><strong>Schools Closed Today</strong><br> <?php $conn1 = mysql_connect("localhost", "username", "password"); if (!$conn1) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("cancellations")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $sql1 = "SELECT date, type, event, action, comments FROM cancellations WHERE type = 'school' AND action = 'closed' AND date = CURDATE() "; $result1 = mysql_query($sql1); if (!$result1) { echo "Could not successfully run query ($sql1) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result1) == 0) { echo "</strong>There aren't any schools closed for today!"; exit; } // While a row of data exists, put that row in $row as an associative array // Note: If you're expecting just one row, no need to use a loop while ($row1 = mysql_fetch_assoc($result1)) { echo $row1["event"]; echo " "; echo $row1["comments"]; echo "<br>"; } mysql_free_result($result1); ?> </p> <p><strong>Schools Running Two Hours Late Today</strong><br> <?php $conn2 = mysql_connect("localhost", "username", "password"); if (!$conn2) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("cancellations")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $sql2 = "SELECT date, type, event, action, comments FROM cancellations WHERE type = 'school' AND action = '2 Hours Late' AND date = CURDATE() "; $result2 = mysql_query($sql2); if (!$result2) { echo "Could not successfully run query ($sql2) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result2) == 0) { echo "</strong>There aren't any schools running 2 hours late today!"; exit; } // While a row of data exists, put that row in $row as an associative array // Note: If you're expecting just one row, no need to use a loop while ($row2 = mysql_fetch_assoc($result2)) { echo $row2["event"]; echo " "; echo $row2["comments"]; echo "<br>"; } mysql_free_result($result2); ?>
  7. Thank you! I should I figured out that using quotes around CURDATE() would look for text, not the mysql command! Thanks again!
  8. I might be barking up the wrong tree, but I can't find much on Google about the syntax of a multiple WHERE statement. Here's my code: <?php $conn = mysql_connect("localhost", "username", "password"); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("cancellations")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $sql = "SELECT date, type, event, action, comments FROM cancellations WHERE (type = 'school' AND date = 'CURDATE()' )"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "</strong></br>There aren't any weather realated announcements for today!"; exit; } // While a row of data exists, put that row in $row as an associative array // Note: If you're expecting just one row, no need to use a loop while ($row = mysql_fetch_assoc($result)) { echo $row["event"]; echo $row["action"]; echo $row["comments"]; echo "<br>"; } mysql_free_result($result); ?> On the line: $sql = "SELECT date, type, event, action, comments FROM cancellations WHERE (type = 'school' AND date = 'CURDATE()' )"; I can remove the second clause, and the code works just fine. Can someone please point me in the right direction on a multiple WHERE syntax? Thanks. Brian
  9. [!--quoteo(post=361878:date=Apr 5 2006, 02:59 AM:name=Napoleon001)--][div class=\'quotetop\']QUOTE(Napoleon001 @ Apr 5 2006, 02:59 AM) [snapback]361878[/snapback][/div][div class=\'quotemain\'][!--quotec--] Something like: [code] switch ($_REQUEST['recipient']) {    case "joe bloggs": $recipient = "joe.bloggs@hismail.com"; break;    case "jenny whoever": $recipient = "jenny.whoever@hermail.com"; break; } [/code] [a href=\"http://www.tizag.com/phpT/switch.php\" target=\"_blank\"]This is a good site that I found and now use when my brain has a blank moment.[/a] It's easier that the PHP official website most of the time. Hope that helps a bit. [/quote] That did the trick...thanks.
  10. I have a PHP form mail script that works just fine. Now, though, I need the user to be able to select the recipient. No, problem, just use a drop box and pass the address that way. WRONG. I want to be able to hide the email address (for obvious reasons) and I can't figure out the logic on how to hide it. I've tried passing a variable like this in the drop box: [code] <select size="1" name="recipient"> <option>Please Select Recipient</option> <option value="$name1">First Person's Name</option> <option value="$name2">Second Person's Name</option> </select> [/code] And then declare the variable in the processing script this way: [code]   $recipient = $_REQUEST['recipient'];    $name = $_REQUEST['name'];   $address = $_REQUEST['address'];     $city = $_REQUEST['city'];   $state = $_REQUEST['state'];     $country = $_REQUEST['country'];     $email = $_REQUEST['email'];   $question = $_REQUEST['question']; $name1 = "address1@domain.com"; $name2 = "address2@domain.com"; mail ( "$recipient", "Subject",   "Name: $name\n   Address: $address\n   City: $city\n   State: $state\n   Country: $country\n   Question:$question\n",   "From: $email" );    echo '<meta http-equiv=Refresh content=1;url="http://www.url.com/thankyou.php">'; ?> [/code] But that didn't work...it left the $recipient variable blank. Where am I off in my logic for this to work? Thanks in advance for the help. Brian
  11. [!--quoteo(post=360258:date=Mar 31 2006, 12:29 AM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Mar 31 2006, 12:29 AM) [snapback]360258[/snapback][/div][div class=\'quotemain\'][!--quotec--] Alternatively, you can add a UNIQUE index of the column, and see if the INSERT fails. [/quote] That did the trick (didn't even know that feature existed!)...thanks!
  12. I have the following code to insert data into a database. I'd like to add some data checking so that if the email address is already in the database, it won't let you add another record. The problem is, I don't know where to start. Can somebody point me in the right direction? Thanks. [code] <? $DBhost = "xxx"; $DBuser = "xxx"; $DBpass = "xxx"; $DBName = "kitchen"; mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database"); @mysql_select_db("$DBName") or die("Unable to select database $DBName"); $sqlquery = "INSERT INTO kitchen VALUES('$_POST[name]', '$_POST[address]', '$_POST[city]', '$_POST[state]', '$_POST[phone]', '$_POST[email]', '$_POST[age]')"; $results = mysql_query($sqlquery);   if ($results){     echo "<p>Thank you, your entry has been submitted!</p>";   }   else{     echo mysql_errno().": ".mysql_error()."<BR>";   }   mysql_close (); ?>   [/code]
  13. That did it...thanks SO much for the help.
  14. Here's the code now in edit_db.php [code] mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database"); @mysql_select_db("$DBName") or die("Unable to select database $DBName"); $event=$_POST[event]; $hrs=$_POST[hrs]; $closed=$_POST[closed]; $notes=$_POST[notes]; $row_number=$_POST[row_number]; for ($i=0;$i<count($_POST[event]);$i++){ $usql = "UPDATE weather SET event='$event[$i]', hrs='$hrs[$i]', closed='$closed[$i]', notes='$notes[$i]' WHERE row_number='$row_number[$i]'"; // compiles query } echo "$usql"; //$dosql = mysql_query($usql); // executes query //  if ($dosql){ //    echo "<p>Thank You, your entry has been submitted!</p> //    <meta http-equiv=Refresh content=1;url='http://www.kkbjam.com/weather/edit.php'>"; //  } //  else{ //    echo mysql_errno().": ".mysql_error()."<BR>"; //  } //  mysql_close (); ?>   [/code] Here's the result: UPDATE weather SET event='school 2', hrs='2 Hours Late', closed='', notes='school 2 notes' WHERE row_number='2' Still only the second record...thanks again for the help on this!!!
  15. I did what you said, and it's only updating the last record again!
  16. Thanks, jworisek, for the help, but it isn't updating anything now...not even that last record. edit.php [code] //do your normal mysql setup and connection calls   $dbc = mysql_pconnect('xxx','xxx','xxx');   mysql_select_db('weather',$dbc);   //now get stuff from a table   $sql = "SELECT row_number, event, hrs, closed, notes FROM weather WHERE today=CURDATE()";   ?>    <img src="/images/1360am.jpg" width="180" height="82"><strong><img src="/images/realcountry.jpg" width="180" height="82"><img src="/images/mix.JPG" width="180" height="82"><br> <br> RP Broadcasting Weather Related Announcements for <?php $day = date("F j, Y"); echo "$day"; ?> </strong><br>       <?php          $dbq = mysql_query($sql,$dbc); //now spit out the table and rows for the table        $rs = mysql_query($sql,$dbc);   $matches = 0;   ?> <table width='100%' border='1'> <form method="POST" action="edit_db.php">   <p style="margin-bottom: 0"><br>     <font color="#000000">     <br>     <br>     </font></p>       <tr>        <td><div align="center">Event or School</div></td>       <td><div align="center">2 Hours Late?</div></td>       <td><div align="center">Closed?</div></td>       <td><div align="center">Notes - when rescheduled or when open</div></td>     </tr>     <tr>      <?php       while ($row = mysql_fetch_assoc($rs)) { $matches++; ?>                   <input name="row_number[]" type="hidden" id="row_number" value="<?php echo"$row[row_number]" ?>" />                 <td><div align="center"><font color="#000000">            <input name="event[]" type="text" id="event" size="50" maxlength="200"  value="<?php echo"$row[event]" ?>" />           </font></div></td>       <td><div align="center">            <input name="hrs[]" type="text" id="hrs" size="11" maxlength="15"  value="<?php echo"$row[hrs]" ?>" />         </div></td>      <td><div align="center">            <input name="closed[]" type="text" id="closed" size="7" maxlength="7"  value="<?php echo"$row[closed]" ?>" />       </div></td>       <td><div align="center"><font color="#000000">           <input name="notes[]" type="text" id="notes" size="50" maxlength="200"  value="<?php echo"$row[notes]" ?>" />           </font></div></td> </TR> <?php }   if (! $matches) { echo ("</table>There are no weather related announcements today"); }   echo "</TABLE>";   ?>     <input name="submit" type="submit" value="Submit" />     <input type="reset" name="Reset" value="Reset" /> </form> [/code] and edit_db.php [code] mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database"); @mysql_select_db("$DBName") or die("Unable to select database $DBName"); for ($i=0;$i<count($_POST[event]);$i++){ $usql = "UPDATE weather SET event='$_POST[event][$i]', hrs='$_POST[hrs]', closed='$_POST[closed]', notes='$_POST[notes]' WHERE row_number='$_POST[row_number][$i]'"; // compiles query } $dosql = mysql_query($usql); // executes query   if ($dosql){     echo "<p>Thank You, your entry has been submitted!</p>     <meta http-equiv=Refresh content=1;url='http://www.mysite.com'>";   }   else{     echo mysql_errno().": ".mysql_error()."<BR>";   }   mysql_close (); ?>   [/code] If I echo $usql this is what I get: [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--] UPDATE weather SET event='Array[1]', hrs='Array', closed='Array', notes='Array' WHERE row_number='Array[1]' [/quote] Thanks again for the help. Brian
  17. so how should I design this so that it works the way that I want?
  18. Here's the html of the "edit.php" page. [code] //do your normal mysql setup and connection calls   $dbc = mysql_pconnect('xxx','xxx','xxx');   mysql_select_db('weather',$dbc);   //now get stuff from a table   $sql = "SELECT row_number, event, hrs, closed, notes FROM weather WHERE today=CURDATE()";   ?>    <img src="/images/1360am.jpg" width="180" height="82"><strong><img src="/images/realcountry.jpg" width="180" height="82"><img src="/images/mix.JPG" width="180" height="82"><br> <br> RP Broadcasting Weather Related Announcements for <?php $day = date("F j, Y"); echo "$day"; ?> </strong><br>       <?php          $dbq = mysql_query($sql,$dbc); //now spit out the table and rows for the table        $rs = mysql_query($sql,$dbc);   $matches = 0;   ?> <table width='100%' border='1'> <form method="POST" action="edit_db.php">   <p style="margin-bottom: 0"><br>     <font color="#000000">     <br>     <br>     </font></p>       <tr>        <td><div align="center">Event or School</div></td>       <td><div align="center">2 Hours Late?</div></td>       <td><div align="center">Closed?</div></td>       <td><div align="center">Notes - when rescheduled or when open</div></td>     </tr>     <tr>      <?php       while ($row = mysql_fetch_assoc($rs)) { $matches++; ?>                   <input name="row_number" type="hidden" id="row_number" value="<?php echo"$row[row_number]" ?>" />                 <td><div align="center"><font color="#000000">            <input name="event" type="text" id="event" size="50" maxlength="200"  value="<?php echo"$row[event]" ?>" />           </font></div></td>       <td><div align="center">            <input name="hrs" type="text" id="hrs" size="11" maxlength="15"  value="<?php echo"$row[hrs]" ?>" />         </div></td>      <td><div align="center">            <input name="closed" type="text" id="closed" size="7" maxlength="7"  value="<?php echo"$row[closed]" ?>" />       </div></td>       <td><div align="center"><font color="#000000">           <input name="notes" type="text" id="notes" size="50" maxlength="200"  value="<?php echo"$row[notes]" ?>" />           </font></div></td> </TR> <?php }   if (! $matches) { echo ("</table>There are no weather related announcements today"); }   echo "</TABLE>";   ?>     <input name="submit" type="submit" value="Submit" />     <input type="reset" name="Reset" value="Reset" /> </form> [/code] The row_number is a hidden field here that again is primary key, auto increment in the database. So, the first record in the database is "1" and the second is "2" and so on. If I echo that back in this html, it does display the correct information. Am I way off base in my logic, or am I on the right track?
  19. I'm having a hard time figuring out how to setup my WHERE statement to update multiple records. Here's what I have: [code] mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database"); @mysql_select_db("$DBName") or die("Unable to select database $DBName"); $usql = "UPDATE weather SET event='$_POST[event]', hrs='$_POST[hrs]', closed='$_POST[closed]', notes='$_POST[notes]' WHERE $_POST[row_number] = row_number"; // compiles query $dosql = mysql_query($usql); // executes query   if ($dosql){     echo "<p>Thank You, your entry has been submitted!</p>       <meta http-equiv=Refresh content=1;url='http://www.mysite.com/'>";   }   else{     echo mysql_errno().": ".mysql_error()."<BR>";   }   mysql_close (); [/code] The "row_number" field is set to primary key, auto increment. The problem with the code is that it's only updating the LAST record, not all the other records that need updating. Can someone point me in the right direction on how to word the WHERE statement? Thanks.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.