Jump to content

While Loop


june_c21

Recommended Posts

hi, i try to run this code but appear error. can someone help me figure out what is the problem? thanks a lot

 

<?php
session_start();
$host = 'localhost';
$user = 'root';
$password = 'admin';
$dbase = 'eclaim';

$dblink = mysql_connect($host,$user,$password);
mysql_select_db($dbase,$dblink);

$event_desc = $_POST['event_desc'];
$pic_id = $_POST['pic_id'];
$venue = $_POST['venue'];
$staffs_involved = $_POST['staffs_involved'];
$person = $_POST ['person'];

while ($event_desc != NULL)
{
$query= "INSERT INTO add1( event_desc,pic_id,venue,staffs_involved,person) VALUES ('$event_desc','$pic_id','$venue','$staffs_involved','$person')";
$result = mysql_query($query,$dblink);

}

$redirect = 'main.php';
header ("location: $redirect");

?>

Link to comment
Share on other sites

what are you trying to do with:

 

while ($event_desc != NULL)
{

 

careful - this could create an indefinite loop.

 

 

try:

 

if($event_desc != NULL)
{
$query= "INSERT INTO add1( event_desc,pic_id,venue,staffs_involved,person) VALUES ('$event_desc','$pic_id','$venue','$staffs_involved','$person')";
$result = mysql_query($query,$dblink) or die("Query: ".$query."<br>".mysql_error());

echo($query);

}else{
echo("\$event_desc == null");
}

 

hope this helps,

Link to comment
Share on other sites

event desc is either going to be NULL or NOT NULL for the entire script no matter how many time you loop it unless you unset() it after the loop, in which case you dont need a loop at all...

 

Why do you want to stop insert when event_desc is null, please try to explain your problem and idea and fully and as plainly as possible.

 

thanks,

 

(did you try my solution? what did the page say (exactly, copy+paste if you have to)

 

,

Link to comment
Share on other sites

ya, i try but still no data insert into database.

 

ok, here is my html code

 

<!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=iso-8859-1" />
<title>KEV Calendar</title>
   <link rel="stylesheet" href="http://www.zapatec.com/website/main/../ajax/zpcal/themes/winter.css"  />
<style type="text/css">
<!--
.style8 {font-family: Arial; font-weight: bold; font-size: 14px; }
body {
background-color: #FFFF99;
}
.style12 {font-family: Arial; font-weight: bold; font-size: 12px; color: #FF3300; }
.style13 {
font-family: Arial;
font-weight: bold;
}



-->
</style>

<script type="text/javascript" src="http://www.zapatec.com/website/main/../ajax/zpcal/../utils/zapatec.js"></script>
    <script type="text/javascript" src="http://www.zapatec.com/website/main/../ajax/zpcal/src/calendar.js"></script>
<!-- Loading language definition file -->
    <script type="text/javascript" src="http://www.zapatec.com/website/main/../ajax/zpcal/lang/calendar-en.js"></script>


</head>

<body>
<p class="style8"> </p>
<form id="form1" name="form1" method="post" action="add.php">
  <label></label>
  <p>
    <label></label>
  <span class="style13">KEV Calender  </span></p>
  <table width="516" height="138" border="1">

    <tr>
      <td><span class="style12">
        <label>Event </label>
      </span></td>
      <td><textarea name="event_desc" id="event_desc"></textarea></td>
          <td class="style12"><p>Registered By (staff no.)100xxxxx / skxxxxx </p>      </td>
      <td><input name="pic_id" type="text" id="pic_id" /></td>

      <td class="style12">Venue</td>
      <td><input name="venue" type="text" id="venue" /></td>

      <td><span class="style12">
        <label>Staff Involved</label>
      </span></td>
      <td><input name="staffs_involved" type="text" id="staffs_involved" /></td>

      <td><span class="style12">
        <label>Person In Charge </label>
      </span></td>
      <td><input name="person" type="text" id="person" /></td>
    </tr>
    <tr>
      <td><span class="style12">
        <label>Event </label>
      </span></td>
      <td><textarea name="event_desc" id="event_desc"></textarea></td>
          <td class="style12"><p>Registered By (staff no.)100xxxxx / skxxxxx </p>      </td>
      <td><input name="pic_id" type="text" id="pic_id" /></td>

      <td class="style12">Venue</td>
      <td><input name="venue" type="text" id="venue" /></td>

      <td><span class="style12">
        <label>Staff Involved</label>
      </span></td>
      <td><input name="staffs_involved" type="text" id="staffs_involved" /></td>

      <td><span class="style12">
        <label>Person In Charge </label>
      </span></td>
      <td><input name="person" type="text" id="person" /></td>
    </tr>
    <tr>
      <td><span class="style12">
        <label>Event </label>
      </span></td>
      <td><textarea name="event_desc" id="event_desc"></textarea></td>
          <td class="style12"><p>Registered By (staff no.)100xxxxx / skxxxxx </p>      </td>
      <td><input name="pic_id" type="text" id="pic_id" /></td>

      <td class="style12">Venue</td>
      <td><input name="venue" type="text" id="venue" /></td>

      <td><span class="style12">
        <label>Staff Involved</label>
      </span></td>
      <td><input name="staffs_involved" type="text" id="staffs_involved" /></td>

      <td><span class="style12">
        <label>Person In Charge </label>
      </span></td>
      <td><input name="person" type="text" id="person" /></td>
    </tr>
  </table>

  <p>
    <label>
    <input type="submit" name="Submit" value="Enter" />
    </label>
  </p>
</form>
<p> </p>
</body>
</html>

 

user will input data according to rows. For example, user A will input for 2 rows only and user input for all rows. So i want to stop inserting data when the rows is empty. Since $event_desc is the first column, so i using when $event_desc is empty (NULL) stop inserting data into database

Link to comment
Share on other sites

im sorry but i dont understand you

 

user will input data according to rows. For example, user A will input for 2 rows only and user input for all rows. So i want to stop inserting data when the rows is empty. Since $event_desc is the first column, so i using when $event_desc is empty (NULL) stop inserting data into database

 

can you explain a little better?

Link to comment
Share on other sites

take it step by step.

 

First add "or die(mysql_error())" on your db query to see if there is an error.

 

If there is no error, remove the while statement completely and see if that works.

 

 

You do understand that when a form is submitted to your php script, the $_POST's will only come through once? A while loop shouldn't be of any use at all.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.