SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2003
    Location
    sydney
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql flow control help

    Can someone tell me why this script writes to the database on loading with blank values and how to stop this.

    This script loads after from another one and $cid and $cust variables are carried across.

    Basically, i would like it to write to the database only when the submit button is pressed.

    It allows me to write to the database once loaded with values entered.

    I have another similar script that works how i want it to, but this one is driving me nuts.

    Any way here goes:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>add-jobs</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <?php

    $cid=$_POST['cid'];
    $cust=$_POST['customername'];

    ?>
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    <?php echo ("Customerinput name='customer' type='text' value='$cust'/>\n");
    echo("<input name='cid' type='hidden' value='$cid'/>\n");?>
    Job Nameinput name="jobname" type="text">
    <input name="submit" type="submit" value="submit"/>
    </form>
    <?php

    // Connect to the database server
    $dbcnx = @mysql_connect('localhost', 'root', 'mypassword');
    if (!$dbcnx) {
    die( '<p>Unable to connect to the ' .
    'database server at this time.</p>' );
    }
    // Select the haec database
    if (! @mysql_select_db('haec') ) {
    die( '<p>Unable to locate the haec ' .
    'database at this time.</p>' );
    }

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

    $customername = ucwords(strtolower($_POST['customer']));
    $jobname = ucwords(strtolower($_POST['jobname']));
    $cid = $_POST['cid'];
    $sql = "INSERT INTO projects SET
    Customer='$customername',
    JobName='$jobname',
    cid='$cid' ";
    if (@mysql_query($sql)) {
    echo('<p><em><font color="#0000FF">'.$jobname.'</font></em> has been added to the <em><font color="#0000FF">'.$customername.'</font></em> jobs database list.</p>');
    } else {
    echo('<p>Error adding the customer: ' .
    mysql_error() . '</p>');
    }
    }
    ?>
    Press here if submission was succesful:
    <form action="customers-jobs.php" method="post">
    <?php echo("<input name='cid' type='hidden' value='$cid'/>\n");
    echo("<input name='id' type='hidden' value='$cust'/>\n");?>
    <input name="submit1" type="submit" value="here">
    </form>

    </body>
    </html>

  2. #2
    SitePoint Member
    Join Date
    Apr 2003
    Location
    sydney
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by seno
    Can someone tell me why this script writes to the database on loading with blank values and how to stop this.

    This script loads after from another one and $cid and $cust variables are carried across.

    Basically, i would like it to write to the database only when the submit button is pressed.

    It allows me to write to the database once loaded with values entered.

    I have another similar script that works how i want it to, but this one is driving me nuts.

    Any way here goes:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>add-jobs</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <?php

    $cid=$_POST['cid'];
    $cust=$_POST['customername'];

    ?>
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    <?php echo ("Customerinput name='customer' type='text' value='$cust'/>\n");
    echo("<input name='cid' type='hidden' value='$cid'/>\n");?>
    Job Nameinput name="jobname" type="text">
    <input name="submit" type="submit" value="submit"/>
    </form>
    <?php

    // Connect to the database server
    $dbcnx = @mysql_connect('localhost', 'root', 'mypassword');
    if (!$dbcnx) {
    die( '<p>Unable to connect to the ' .
    'database server at this time.</p>' );
    }
    // Select the haec database
    if (! @mysql_select_db('haec') ) {
    die( '<p>Unable to locate the haec ' .
    'database at this time.</p>' );
    }

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

    $customername = ucwords(strtolower($_POST['customer']));
    $jobname = ucwords(strtolower($_POST['jobname']));
    $cid = $_POST['cid'];
    $sql = "INSERT INTO projects SET
    Customer='$customername',
    JobName='$jobname',
    cid='$cid' ";
    if (@mysql_query($sql)) {
    echo('<p><em><font color="#0000FF">'.$jobname.'</font></em> has been added to the <em><font color="#0000FF">'.$customername.'</font></em> jobs database list.</p>');
    } else {
    echo('<p>Error adding the customer: ' .
    mysql_error() . '</p>');
    }
    }
    ?>
    Press here if submission was succesful:
    <form action="customers-jobs.php" method="post">
    <?php echo("<input name='cid' type='hidden' value='$cid'/>\n");
    echo("<input name='id' type='hidden' value='$cust'/>\n");?>
    <input name="submit1" type="submit" value="here">
    </form>

    </body>
    </html>
    Fixed it already - forgot about GET.

    seno


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •