Jump to content

perseas

Member
  • Posts

    10
  • Joined

  • Last visited

Posts posted by perseas

  1. Still getting errors? Or no errors any more, but the dropown list doesn't have any items in it?

     

    The drop down list is empty. And the error log has two notices

    [14-Oct-2013 18:59:37 UTC] PHP Notice: Uninitialized string offset: 1 in C:\xampp\htdocs\haa\ben.php on line 51

     

    [14-Oct-2013 18:59:37 UTC] PHP Notice: Uninitialized string offset: 1 in C:\xampp\htdocs\haa\ben.php on line 51

     

    pointing this command:

     

    <option value="<?php $array[$k]?>"><?php $array[$k]?></option>

     

    (all the code is updated at the previous post)

  2. For this line:

     

    for ($k=0;$k<=$count;$k++)?>

    You need to use:

     

    for ($k=0;$k<$count;$k++)?>

    (Note the "<" not "<=". This is because count() returns a whole number, but arrays start at key 0. So if count() returns 3, you only have three array items, and you'd need to loop array items 0-2.

     

    No, you don't need to have another mysql_query. I was just confused by your $row / $array typo.

    Thanks for the correction, but still the list is empty. I can't understand why..

  3. First thing I notice: Where is $row['$k'] being set within the renderForm() function? I see where it's intended to be set, but it looks like it has been commented out? Is that supposed to be $row?

     

    Secondly, why is there any need to pass around the number of elements in the array? This likely doesn't have anything to do with your error, but it's just adding unnecessary complexity to your code. For example, you could simplify the retreival of the data:

     

    <?php

    $result = mysql_query("SELECT * FROM users",$link);

    while($row = mysql_fetch_array($result))

    $array[]=$row['surname'];

    }

    //for ($k=0;$k<count($array);$k++) echo $array[$k]; // I can see the content of $array. It's right.

    ?>

     

    Then, you can do the same thing with the renderForm() function, using count(), and there's no need to pass around $i.

     

     

    Ι corrected the code with the count command. Αnd I edited the previous post with my code.

    For your first notice. I didn't undesrtand. I have already the $array with the data. Is it necessary to have a mysql_query again? This is why I commented out.

    about the $row['$k'], it was my mistake, I corrected it.

  4. The new code. the data are retrieved (succesfully) outside the renderform, and i pass them inside the function.

     

     

    <?php //add_flight.php

    session_start();

    $hostname = "myhostname";

    $database = "mydatabase";

    $username = "username";

    $password = "password";

    $link = mysql_connect( $hostname , $username , $password ) or die("Attention! Server Connection Problem : " . mysql_error());

    mysql_select_db($database,$link);

    ?>

     

    <?php

    $i=0;

    $result = mysql_query("SELECT * FROM users",$link);

    while($row = mysql_fetch_array($result))

    $array[$i]=$row['name'];

     

    //for ($k=0;$k<=$i;$k++) echo $array[$k]; // I can see the content of $array. It's right.

    ?>

     

     

     

     

    <?php

    function renderForm($fdate, $pic, $planetype,$link, $array,$error)

    {

    ?>

    <html>

    <head>

    <title>Add New Flight</title>

    </head>

    <body>

    <?php

    if ($error != '')

    {

    echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';

    }

    ?>

    <?/*php $result = mysql_query("SELECT * FROM users",$link)or die(mysql_error());

    $row == mysql_fetch_array($result);

    echo $row[name];*/?>

     

    <form action="add_flight.php" method="post">

    <table>

    <tr><td><B>Date:</B></td><td><input type="text" name="fdate"value="<?php echo $fdate; ?>"/></td></tr>

    <tr><td><B>PIC:</B></td><td><select name="pic"value="<?php echo $pic; ?>"/>

    <?php

    $count=count($array);

    for ($k=0;$k<$count;$k++)?>

    <option value="<?php $array['$k']?>"><?php $array['$k']?></option> //// line 48

     

     

    ?>

    </select>

    </td></tr>

    <tr><td><B>planetype:</B></td><td><select name="planetype"value="<?php echo $planetype; ?>"/>

    <option value="cessna">cessna</option>

    <option value="piper">piper</option>

    <option value="diamond">diamond</option>

    </select>

    </td></tr>

    <input type="submit" name="submit" value="Submit">

    </form>

    </body>

    </html>

     

    <?php

    }

    if (isset($_POST['submit']))

    {

    $fdate = mysql_real_escape_string(htmlspecialchars($_POST['fdate']));

    $planetype = mysql_real_escape_string(htmlspecialchars($_POST['planetype']));

    $pic = mysql_real_escape_string(htmlspecialchars($_POST['pic']));

     

    if ($fdate == '' || $pic == '')

    {

    $error = 'ERROR: Please fill in all required fields!';

     

    renderForm($fdate, $pic, $planetype,$link, $array,$error);

    }

    else

    {

    mysql_query("INSERT flights SET fdate='$fdate', pic='$pic', planetype='$planetype'") or die(mysql_error());

     

    header("Location: add_flight.php");

    }

    }

    else

    {

    renderForm('','','', '','', '');

    }

    ?>

     

    error log

    [14-Oct-2013 18:52:22 UTC] PHP Notice: Uninitialized string offset: 1 in C:\xampp\htdocs\haa\ben.php on line 48

     

    [14-Oct-2013 18:52:22 UTC] PHP Notice: Uninitialized string offset: 1 in C:\xampp\htdocs\haa\ben.php on line 48

  5. That's not the way $global works though. See http://php.net/manual/en/language.variables.scope.php

     

    I believe if you include the line

     

    global $link;

     

    within your renderForm function, right before you use it, it should work fine.

     

    $global link; added in the code. Only one warning, but nothing happened. I'm sending in a few minutes the code you asked for...(retrieve data outside the renderform)

    PHP Warning: mysql_query() expects parameter 2 to be resource, string given in C:\xampp\htdocs\haa\ben.php on line 26

  6. Where were you trying to declare $link as a global variable? Directly before you used it, within the renderForm() function?

     

    Realistically though... at least, according to the way I had organized my code... that renderForm should do just that -- render the form. Personally, I would access the database and retrieve the data you need outside of the form, store that data in an array, and pass it to the renderForm() function. This way, the code is a little cleaner, in the sense that you are separating the logic of the page from the HTML/CSS that gets displayed.

     

    I declare $link as global, over the mysql_connect.

    e.g.

    global $link;

    $link = mysql_connect( $hostname , $username , $password ) or

    die("Attention! Server Connection Problem : " . mysql_error());

     

    I'll post the code in which the access and the data are retrieved outside the form, and stored in an array to the function. Soon!

  7. Hello all! I'm new in PHP and I'm trying to create a dynamic drop-down list. I have two tables, pilots and flights. So, when I want to insert a flight, I'd like to select from a dynamic list with the pilots (i can add pilots, with another php file). But, when I'm trying to run the code, nothing happens. Here is my code:

     

     

     

    <?php //add_flight.php

    session_start();

    $hostname = "myhostname";

    $database = "mydatabase";

    $username = "myusername";

    $password = "mypassword";

    $link = mysql_connect( $hostname , $username , $password ) or die("Attention! Server Connection Problem : " . mysql_error());

    mysql_select_db($database,$link);

    ?>

     

    <?php

    function renderForm($fdate, $pic, $planetype, $error)

    {

    ?>

    <html>

    <head>

    <title>Add New Flight</title>

    </head>

    <body>

    <?php

    if ($error != '')

    {

    echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';

    }

    ?>

     

    <form action="add_flight.php" method="post">

    <table>

    <tr><td><B>Date:</B></td><td><input type="text" name="fdate"value="<?php echo $fdate; ?>"/></td></tr>

    <tr><td><B>PIC:</B></td><td><select name="pic"value="<?php echo $pic; ?>"/>

    <?php

    global $link;

    $result = mysql_query("SELECT * FROM pilots",$link)or die(mysql_error());

    while($row == mysql_fetch_array($result))

    {?>

    <option value="<?php $row['pname']?>"><?php $row['pname']?></option>

    <?php }?>

    </select>

    </td></tr>

    <tr><td><B>planetype:</B></td><td><select name="planetype"value="<?php echo $planetype; ?>"/>

    <option value="cessna">cessna</option>

    <option value="piper">piper</option>

    <option value="diamond">diamond</option>

    </select>

    </td></tr>

    <input type="submit" name="submit" value="Submit">

    </form>

    </body>

    </html>

    <?php

    }

    if (isset($_POST['submit']))

    {

    $fdate = mysql_real_escape_string(htmlspecialchars($_POST['fdate']));

    $planetype = mysql_real_escape_string(htmlspecialchars($_POST['planetype']));

    $pic = mysql_real_escape_string(htmlspecialchars($_POST['pic']));

     

    if ($fdate == '' || $pic == '')

    {

    $error = 'ERROR: Please fill in all required fields!';

     

    renderForm($fdate, $pic, $planetype, $error);

    }

    else

    {

    mysql_query("INSERT flights SET fdate='$fdate', pic='$pic', planetype='$planetype'") or die(mysql_error());

     

    header("Location: add_flight.php");

    }

    }

    else

    {

    renderForm('','','', '');

    }

    ?>

     

    Here is the php_error_log:

    PHP Notice: Undefined variable: link in C:\xampp\htdocs\haa\ben.php on line 32

    PHP Warning: mysql_query() expects parameter 2 to be resource, null given in C:\xampp\htdocs\haa\ben.php on line 32

     

    - I also tried the same code with a second function which retrieves succesfully the data outside the renderform and i passed them in renderform. No luck.

    - I declared $link as a global variable (global $link), nothing happened, but the notice of the error log stayed on.

    - I abbrogated the renderford and modified the code. I can see the data outside the form, but not inside.

     

    Thanks in advance!

  8. It sounds like you have the right general approach. I would do it by accessing the database outside the renderform() function, and creating an array of the data for the dropdown. I would then pass that array into the renderform function, adding the array as an extra parameter:

     

    function renderForm($first, $last, $new_data, $error)

     

    And then within the function, use that data to generate the dropdown.

     

    That's just the form portion, obviously -- you'd need to update the part of the code that handles form submission, and make sure you are saving the selected item from the dropdown correctly.

     

    Thanks for the response! I've tried that, but nothing happened. As I mentioned previously, inside the renderform there's no problem, I can see the data. The problem is that when I reallocate the commands (cut-paste!), inside the form, nothing happens and I don't know why...

  9. Hi all! nice tutorial. It helped to learn some things. I'm new in PHP/MySQL and I have a question. In case I want to modify the new.php to create a dynamic drop down list (with data from another table of the database) in the form, how can I use that? I tried but I failed. When Ι try to retrieve the data outside the form, it's OK, but when I'm trying to relocate the commands into the form this time, nothing happens! why is that? In the form, are there local variables or sth like that? Thanks in advance!

×
×
  • Create New...