Jump to content

Herbert at GC

Member
  • Posts

    15
  • Joined

  • Last visited

Posts posted by Herbert at GC

  1. Hi Ben

     

    bind_param()is the problem and the or worked.

     

    problem solved!

     

    Thanks man -- IOU

     

     

    This was also one of the problem. extra comma at the end of the "phone=?"

     

     

    bad: if ($stmt = $mysqli->prepare("UPDATE client_list SET company_name=?, contact=?, phone=?, WHERE id=?"))

     

    good: if ($stmt = $mysqli->prepare("UPDATE client_list SET company_name=?, contact=?, phone=? WHERE id=?"))

     

     

    - Herbert

  2. Two points (I'm not sure if these are causing the issue, but definitely take a look):

     

    if ($company_name == '', $contact == '', $phone == '')

    I'm pretty sure you need to be using "&&" or "||" -- commas (I believe) should cause an error.

     

    and also in this section:

     

    if ($stmt = $mysqli->prepare("INSERT client_list (company_name, contact, phone) VALUES (?, ?, ?)"))
    {
    $stmt->bind_param("ss", $company_name, $contact, $phone);

    I believe the bind_param() function needs to use "sss" rather than "ss", since you are using three variables, not two.

     

    If neither of these things fix the issue, can you also post exactly what issue you are having? When you try to submit the form and create a new entry in the database, what happens? Nothing? Page refresh? Do you get any error messages?

     

     

    Hi Ben

     

    bind_param()is the problem and the or worked.

     

    problem solved!

     

    Thanks man -- IOU

  3. Yes, it is fine to use 3 "or's". That isn't the issue. I think if you need further help with this, you'll need to post the code. There isn't too much I can do to help otherwise (other than just trying to guess at the issue.)

     

     

    Hi Ben: code below.

    ==============================================================

     

    <link rel="stylesheet" href="crud-css.css" type="text/css" />

    <?php

    /*

    Allows the user to both create new records and edit existing records

    */

     

    // connect to the database

    include('crud-connect.php');

     

    // creates the new/edit record form

    // since this form is used multiple times in this file, I have made it a function that is easily reusable

    function renderForm($company_name = '', $contact ='', $phone ='', $error = '', $id = '')

    { ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

    <html>

    <head>

    <title>

    <?php if ($id != '') { echo "Edit Record"; } else { echo "Add Record"; } ?>

    </title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    </head>

    <body>

    <h1><?php if ($id != '') { echo "Edit Record"; } else { echo "Add Record"; } ?></h1>

    <?php if ($error != '') {

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

    . "</div>";

    } ?>

     

    <form action="" method="post">

    <div>

    <?php if ($id != '') { ?>

    <input type="hidden" name="id" value="<?php echo $id; ?>" />

    <p>ID: <?php echo $id; ?></p>

    <?php } ?>

    <table>

    <tr>

    <td><strong>Company Name:</td>

    <td></strong> <input type="text" name="company_name" value="<?php echo $company_name; ?>"/> *</td>

    </tr>

     

    <tr>

    <td><strong>Contact Name:</td>

    <td></strong> <input type="text" name="contact" value="<?php echo $contact; ?>"/> *</td>

    <tr>

    <td><strong>Phone:</td>

    <td></strong> <input type="text" name="phone" value="<?php echo $phone; ?>"/> *</td>

    </tr>

    <tr>

    <td> </td>

    <td>*Required  <input type="submit" name="submit" value="Submit" /></td>

    </tr>

    </table>

    </div>

    </form>

    </body>

    </html>

     

    <?php }

     

     

     

    /*

     

    EDIT RECORD

     

    */

    // if the 'id' variable is set in the URL, we know that we need to edit a record

    if (isset($_GET['id']))

    {

    // if the form's submit button is clicked, we need to process the form

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

    {

    // make sure the 'id' in the URL is valid

    if (is_numeric($_POST['id']))

    {

    // get variables from the URL/form

    $id = $_POST['id'];

    $company_name = htmlentities($_POST['company_name'], ENT_QUOTES);

    $contact = htmlentities($_POST['contact'], ENT_QUOTES);

    $phone = htmlentities($_POST['phone'], ENT_QUOTES);

     

    // check that company_name and contact are both not empty

    if ($company_name == '', $contact == '', $phone == '')

    {

    // if they are empty, show an error message and display the form

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

    renderForm($company_name, $contact, $error, $phone, $id);

    }

    else

    {

    // if everything is fine, update the record in the database

    if ($stmt = $mysqli->prepare("UPDATE client_list SET company_name=?, contact=?, phone=?, WHERE id=?"))

    {

    $stmt->bind_param("ssi", $company_name, $contact, $phone, $id);

    $stmt->execute();

    $stmt->close();

    }

    // show an error message if the query has an error

    else

    {

    echo "ERROR: could not prepare SQL statement.";

    }

     

    // redirect the user once the form is updated

    header("Location: crud-view.php");

    }

    }

    // if the 'id' variable is not valid, show an error message

    else

    {

    echo "Error!";

    }

    }

    // if the form hasn't been submitted yet, get the info from the database and show the form

    else

    {

    // make sure the 'id' value is valid

    if (is_numeric($_GET['id']) && $_GET['id'] > 0)

    {

    // get 'id' from URL

    $id = $_GET['id'];

     

    // get the recod from the database

    if($stmt = $mysqli->prepare("SELECT * FROM client_list WHERE id=?"))

    {

    $stmt->bind_param("i", $id);

    $stmt->execute();

     

    $stmt->bind_result($id, $company_name, $contact, $phone);

    $stmt->fetch();

     

    // show the form

    renderForm($company_name, $contact, $phone, NULL, $id);

     

    $stmt->close();

    }

    // show an error if the query has an error

    else

    {

    echo "Error: could not prepare SQL statement";

    }

    }

    // if the 'id' value is not valid, redirect the user back to the view.php page

    else

    {

    header("Location: view.php");

    }

    }

    }

     

     

     

    /*

     

    NEW RECORD

     

    */

    // if the 'id' variable is not set in the URL, we must be creating a new record

    else

    {

    // if the form's submit button is clicked, we need to process the form

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

    {

    // get the form data

    $company_name = htmlentities($_POST['company_name'], ENT_QUOTES);

    $contact = htmlentities($_POST['contact'], ENT_QUOTES);

    $phone = htmlentities($_POST['phone'], ENT_QUOTES);

     

    // check that company_name and contact are both not empty

    if ($company_name == '' || $contact == '' || $phone == '')

    {

    // if they are empty, show an error message and display the form

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

    renderForm($company_name, $contact, $phone, $error);

    }

    else

    {

    // insert the new record into the database

    if ($stmt = $mysqli->prepare("INSERT client_list (company_name, contact, phone) VALUES (?, ?, ?)"))

    {

    $stmt->bind_param("ss", $company_name, $contact, $phone);

    $stmt->execute();

    $stmt->close();

    }

    // show an error if the query has an error

    else

    {

    echo "ERROR: Could not prepare SQL statement.";

    }

     

    // redirec the user

    header("Location: crud-view.php");

    }

     

    }

    // if the form hasn't been submitted yet, show the form

    else

    {

    renderForm();

    }

    }

     

    // close the mysqli connection

    $mysqli->close();

    ?>

     

    Thanks, Herbert

  4. Your code

     

    if($name =='' || $last =='' || $mycode)"

    currently reads: "if name is null, or last is null, or mycode is true... continue"

     

    I'm guessing it needs to be:

     

    if($name =='' || $last =='' || $mycode == '')

     

     

    Hi Ben,

     

    it is actually if($name =='' || $last == '' || $phone == '').

    I wonder if it's OK to use OR 3x?

     

    I don't want to post the whole codes I'm tying to solve it my self.

     

    Thanks! Herbert at GC.

  5. Hi Guys Help!

     

    This topic is from Complete Web Programmer tutorial (FYI the tutorial is great!).

     

    I'm trying to add a new row to the view.php page, I inserted a new row in PHPMyAdmin.

    I tweaked the codes (record.php), but it won't add the new record. The code currently use if statement

    to check for rows like this "if($name =='' || $last =='')", my code "if($name =='' || $last =='' || $mycode)"

    could the be the problem?

     

    I used OR 3x within if statement.???

     

    Thank you, -- Herbert at GC

  6. Ben bro I hate to say this, but it works!

     

    Thanks again, Herbert

     

     

     

     

    Let me know. I'm guessing that fixing the "=" issue should take care of things. Without the "=", you aren't setting the proper variables and your delete/edit links won't work.

  7. Noting happens no errors or anything. I'll change the code on view.php.

     

    Thanks Ben

     

    One thing to double check that I found when looking over your code... Take a look at these lines in your view.php:

     

    echo "<td><a href='records.php?id" . $row->id . "'>Edit</a></td>";
    echo "<td><a href='delete.php?id" . $row->id . "'>Delete</a></td>";

    I'm pretty sure it is supposed to be:

     

    echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
    echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";

    (note the "=").

  8. Hi guys this topic is from video tutorial (Complete web programing)"PHP CRUD".

    From video tutorial "php_crud" -- videos: mysqli-2 "view.php" and mysqli- "delete.php"

     

    Problem: Can't delete a record (please help codes below).

     

     

    delete.php code

    ===========================================================

    <?php

     

    include('db-connect.php');

     

    if (isset($_GET['id']) && is_numeric($_GET['id']))

    {

    $id = $_GET['id'];

    if($stmt = $mysqli->prepare("DELETE FROM records WHERE id = ? LIMIT 1"))

    {

    $stmt->bind_param("i",$id);

    $stmt->execute();

    $stmt->close();

    }

    else

    {

    echo "Error: Could not prepare sql etatement";

    }

    $mysqli->close();

     

    header("Location: view.php");

    }

    else

    {

    header("Location: view.php");

    }

    ?>

    ===================================================================

     

    view.php code

    ===================================================================

    <?php

     

    include('db-connect.php');

     

    if ($result = $mysqli->query("SELECT * FROM records ORDER BY id"))

    {

    if($result->num_rows > 0);

    {

    echo "<table border='1' cellpadding='10'>";

    echo "<tr><th>ID</th><th>First Name</th><th>Last Name</th><th></th><th></th></tr>";

     

    while ($row = $result->fetch_object())

    {

    echo "<tr>";

    echo "<td>" . $row->id . "</td>";

    echo "<td>" . $row->firstname . "</td>";

    echo "<td>" . $row->lastname . "</td>";

    echo "<td><a href='records.php?id" . $row->id . "'>Edit</a></td>";

    echo "<td><a href='delete.php?id" . $row->id . "'>Delete</a></td>";

     

    echo "</tr>";

    }

    echo "</table>";

    }

    }

    else

    {

    echo "Error: " . $mysqli->error;

    }

     

    $mysqli->close();

     

     

    ?>

     

     

    Thanks, Herbert

  9. Hi Ben,

     

    Thanks for adding validation to the PHP code. Works great!

    I appreciate your help!

     

     

    FYI, I just purchased the Killer PHP complete web programming video tutorial.

    You guys did a great job putting it together! Love it, highly recommended.

     

     

     

    Thanks again,

    Herbert

     

     

     

     

     

     

     

     

     

    Here's one way to handle things... In the PHP processing page, make sure the correct form fields are filled in before emailing. I know you say that Dreamweaver validates the form, but it only validates the form if the user submits it from your contact html page -- if a user tries to access contact.php directly, it will email a blank message because there is no validation.

     

    (make sure to update your email and contact.html page in the below example. The "header()" line near the bottom of the code is used to redirect the user back to your contact form if someone tries to access the .php page directly.)

     

    <?php
    
    if (isset($_POST['name']) && isset($_POST['company']) && isset($_POST['email']))
    {
    $about = htmlentities($_REQUEST['about']."\n", ENT_QUOTES);
    $priority = htmlentities($_REQUEST['priority']."\n", ENT_QUOTES);
    $name = htmlentities($_REQUEST['name']."\n", ENT_QUOTES);
    $email = htmlentities($_REQUEST['email']."\n", ENT_QUOTES);
    $phone = htmlentities($_REQUEST['phone']."\n", ENT_QUOTES);
    $fax = htmlentities($_REQUEST['fax']."\n", ENT_QUOTES);
    $company = htmlentities($_REQUEST['company']."\n", ENT_QUOTES);
    $callme = htmlentities($_REQUEST['callme']."\n", ENT_QUOTES);
    $comments = htmlentities($_REQUEST['comments']."\n", ENT_QUOTES);
    
    $email_message = "About: {$about} Priority: {$priority} Name: {$name} E-mail: {$email} Phone: {$phone} Fax: {$fax} Message: {$comments} Company: {$company} Please call me at: {$callme}";
    
    mail('youremail@sbcglobal.net','GC Feedback', $email_message);
    }
    else
    {
    header("Location: your_contact_page.html");
    }
    
    ?>

    I have also used the htmlentities() function (http://php.net/manual/en/function.htmlentities.php) when getting the data from your form since I feel a bit more comfortable knowing that there's less of a chance that something invalid/malicious can get through.

  10. Sorry, I should have been clearer -- I mean the PHP code that processes the form (contact.php).

     

     

    PHP code below

     

    ---------------------------------------

    <?php

     

    $about = $_REQUEST['about']."\n";

     

    $priority = $_REQUEST['priority']."\n";

     

    $name = $_REQUEST['name']."\n";

     

    $email = $_REQUEST['email']."\n";

     

    $phone = $_REQUEST['phone']."\n";

     

    $fax = $_REQUEST['fax']."\n";

     

    $company = $_REQUEST['company']."\n";

     

    $callme = $_REQUEST['callme']."\n";

     

    $comments = $_REQUEST['comments']."\n";

     

    $email_message = "About: {$about} Priority: {$priority} Name: {$name} E-mail: {$email} Phone: {$phone} Fax: {$fax} Message: {$comments} Company: {$company} Please call me at: {$callme}";

     

    mail('email@sbcglobal.net','GC Feedback', $email_message);

     

    ?>

     

    ----------------------------------------

     

    Again I'm using Dreamweaver to validate the form.

     

    Thanks,

    Herbert

  11. Like I said, if you post the code, we can help.

     

     

     

    Form source code below

     

    -------------------------------------------------------------

     

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

    <div align="right">

    <table width="242" height="212" border="0" align="center" cellpadding="0" cellspacing="0"><tr>

    <td colspan="3" align="left" class="style4"><label><strong><span class="style5 style6 style12"><br/>

    </span><span class="style10">Contact Us</span><br />

    About:</strong>

    <select name="about" id="about">

    <option selected="selected">Click Here</option>

    <option value="Graphic Design">Graphic Design </option>

    <option value="Website Design">Website Design </option>

    <option value="Logo Design">Logo Design</option>

    <option value="Signage">Signage</option>

    <option value="Printed Promotional Materials">Printed Promotional Materials</option>

    <option value="Technical Illustration">Technical Illustration</option>

    <option value="Packaging Design">Packaging Design</option>

    <option value="Website Update, Existing Customer">Website Update, Existing Customer</option>

    <option value="Website Update, New Customer">Website Update, New Customer</option>

    <option value="Labels & Decals">Labels & Decals</option>

    <option value="OEM Custom Die Cut Decals">OEM Custom Die Cut Decals</option>

    <option value="Labels & Decals RND/Prototyping">Labels & Decals RND/Prototyping</option>

    <option value="Membrane Switch Production Engineering">Membrane Switch Production Engineering</ption>

    <option value="Material Data Sheet">Material Data Sheet</option>

    </select><br /></label><br />

    <strong>Priority:</strong><br /> High

    <input type="radio" name="priority" id="priority" value="High" /> Normal

    <input type="radio" name="priority" id="priority2" value="Normal" /> Low

    <input type="radio" name="priority" id="priority3" value="Low" /> <br />

    </label></td></tr>

    <tr><td width="49" class="style4"><div align="right" class="style29">

    <label for="ID_textfieldName_1C7E60E43BF7560"></label><div class="contact">

    <div align="left" class="style19"><p align="right" class="style3">Name:</p>

    </div></div></div></td><td width="1" class="style4"></td>

    <td width="304" class="style4"><div align="left">

    <input name="name" type="text" id="name" size="30" /> </div></td>

    </tr><tr><td class="style4">Company:</td><td class="style4"></td>

    <td class="style4"><input name="company" type="text" id="company" size="30" /></td></tr>

    <tr><td class="style4"><div align="right" class="style29"><label for="ID_textfieldName_1C7E60E5088ED30">

    </label><div class="contact"><div align="left" class="style3">

    <div align="right">E-mail:</div></div></div></div></td><td width="1" class="style4"></td>

    <td width="304" class="style4"><div align="left">

    <input name="email" type="text" id="email" size="30" /></div></td></tr>

    <tr><td class="style4"><div align="right" class="style29">

    <label for="ID_textfieldName_1C7E60E580F5940"> </label>

    <div class="contact"><div align="left" class="style3">

    <div align="right">Phone:</div></div></div></div></td>

    <td width="1" class="style4"></td><td width="304" class="style4">

    <div align="left"><input id="phone" type="text" name="phone" size="30" />

    </div></td></tr><tr><td class="style4"><div align="right" class="style29">

    <label for="ID_textfieldName_1C7E60E5A5EA070"> </label><div class="contact">

    <div align="left" class="style3"><div align="right">Fax:</div></div>

    </div></div></td><td width="1" class="style4"></td>

    <td width="304" class="style4"><div align="left">

    <input id="fax" type="text" name="fax" size="30" />

    </div></td></tr><tr><td colspan="3" class="style4"></td>

    </tr><tr><td height="25" colspan="3" valign="bottom" class="style4"><div align="center">

    <div class="contact"><div align="left" class="style7">

    <font face="Arial">Please enter your message below</font>:</div>

    </div></div></td></tr><tr><td colspan="3"></td></tr><tr>

    <td colspan="3" align="left"><div align="left"><p>

    <textarea name="comments" rows="6" cols="32" id="comments"></textarea><br />

    <input type="submit" onclick="MM_validateForm('name','','R','company','','R','email','','RisEmail');return document.MM_returnValue" value="Send" /><input type="reset" name="Reset" id="button" value="Reset" />

    </p></div></td></tr><tr><td colspan="3"></td></tr></table><br /></div> <div align="right"></div>

    </form>

     

    ------------------------------------------

    Thanks,

    Herbert

  12. In most cases, no, you can't remove the action attribute (unless your contact form redirects to the same page for processing.) However, it sounds like your best bet would be to ensure that the form processing page can't be accessed directly, and someone has to submit the form in order for it to send the email. Perhaps you can post the code from that page, and we can do our best to help you?

     

     

    The contact form is entirely built using HTML, and use PHP as processor.

     

    I think I will have to re-build the contact form using PHP to self submit, but I don't really now much about PHP yet.

     

    Thanks,

    Herbert

  13. If you are getting blank emails from the contact us form, it sounds like your script used to process the form isn't working properly? Ideally, the script you are using should be able to validate your input and make sure that all the fields in the form have been entered properly before sending email.

     

    Hi Ben,

     

    Thanks for your input. I'm using Dreamweaver to validate the form. I kind of think that someone has been viewing my contact page source code, cut and paste the full path to the browser, hit enter (key board) and goes the blank email. The reason I said this is because I was able to re-create it. So I was thinking that if I can hide the action attributes or the value then it will not be visible in the source code.???

     

    Thanks,

    Herbert

×
×
  • Create New...