Jump to content

G3NERALCHRIS

Members
  • Posts

    21
  • Joined

  • Last visited

Posts posted by G3NERALCHRIS

  1. This usually means PHP encountered an unrecoverable error. First step is to checked the error logs for your server. Or enable error reporting in the php.ini

    error_reporting = E_ALL
    display_errors = On
    
    

    Make sure you restart your http server after making any changes to the php.ini

     

    Post the errors you get here

     

    If it worked on Windows then it should work on your Ubuntu server. I have a feeling something is not configured correctly on Ubuntu. Once we know what the errors are we can start to assist you better.

    I check the php.ini in terminal display errors is on by default and error reporting is E_ALL by default. Any suggestion?

  2. I have the complete website, and works fine on windows using XAMPP control panel accessing via localhost.

     

    Now I am having trouble accessing the table(s) via ubuntu after turning on mysql in ubuntu also I am receving no errors

    just a blank page. You can check the php but it might not make any difference.

     

    All assoicated files for linking with mysql are below if needed I will upload whole webiste without images.

     

    I have stopped mysql and restarted it, over and over again. Also tried using

    sudo /usr/sbin/mysqld --skip-grant-tables --skip networking &

    command and no luck.

    Website is running succesfully just mysql is not working any suggestion on what could be wrong.

    add-aboutme.php

    add-contactme.php

    add-guestbook.php

    delete-contact.php

    guestbook.php

    view-aboutme.php

    view-contactme.php

    view-guestbook.php

  3. I have created a add-login.php and add-reg.php. I'm trying to figure out why it doesn't say to my table in mysql.

    Add login below.

    <!DOCTYPE html>
    <html>
    <head>
    <title>Home Page</title>
    
    <style type="text/css">
    @import url("templatemo_style.css");
    </style>
    </head>
    <body>
    	<div id="page">
    		<div id="logo">
    		</div>
    <html>
    <body>
    <p align="center"><a href="add-reg.php">Register</a>, or enter your user name and password:</p>
    
    <form action="index.html" method="post">
    		
    		<p align="center"> Username: <input type = "text" name="User"><br>
    		<p align="center"> Password: <input type = "password" name="Pass"><br>
    		<br>
    		</br>
    		<input type="submit" value="Login"/>
    		<input type="reset">
    
    </form>
    </body>
    </html>
    </body>
    </html>
    

    add-reg.php

    <!DOCTYPE html>
    <html>
    <head>
    <title>Home Page</title>
    
    <style type="text/css">
    @import url("templatemo_style.css");
    </style>
    </head>
    <body>
    <div id="page">
    <div id="logo"></div>
    <?php 
    $host= "localhost";
    $user = "root";
    $passwd = "";
    $database = "test";
    $tbl_name = "PASS";
    
     mysql_connect($host, $user, $passwd) or die(mysql_error()); 
     mysql_select_db("test") or die(mysql_error()); 
     
     
     //This code runs if the form has been submitted
     if (isset($_POST['submit'])) { 
     
     //This makes sure they did not leave any fields blank
     if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
      die('You did not complete all of the required fields, please go back, and complete the missing fields!');
      }
      
     // checks if the username is in use
      if (!get_magic_quotes_gpc()) {
      $_POST['username'] = addslashes($_POST['username']);
      }
     $usercheck = $_POST['username'];
     $check = mysql_query("SELECT username FROM PASS WHERE username = '$usercheck'") 
    or die(mysql_error());
     $check2 = mysql_num_rows($check);
     
     //if the name exists it gives an error
     if ($check2 != 0) {
      die('Sorry, the username '.$_POST['username'].' is already in use.');
      }
     
     // this makes sure both passwords entered match
      if ($_POST['pass'] != $_POST['pass2']) {
      die('Your passwords did not match. ');
      }
      
      // here we encrypt the password and add slashes if needed
      $_POST['pass'] = md5($_POST['pass']);
      if (!get_magic_quotes_gpc()) {
      $_POST['pass'] = addslashes($_POST['pass']);
      $_POST['username'] = addslashes($_POST['username']);
      }
      
     // now we insert it into the database
      $insert = "INSERT INTO PASS (username, password)
      VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
      $add_member = mysql_query($insert);
      ?>
    
     <h1>Registered</h1>
    
     <p>Thank you very much, you have registered - you may now <a href="add-login.php">login</a>.</p>
    
     <?php 
     } 
     else 
     { 
     ?>
    
     <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    
     <table border="0">
     
     <tr><td colspan=2><h1>Register to access the website.</h1></td></tr> 
    
     <tr><td>Username:</td><td><br>
    
     <input type="text" name="username" maxlength="60">
    
     </td></tr>
    
     <tr><td>Password:</td><td><br>
    
     <input type="password" name="pass" maxlength="10">
    
     </td></tr>
    
     <tr><td>Confirm Password:</td><td><br>
    
     <input type="password" name="pass2" maxlength="10">
    
     </td></tr>
    
     <tr><th colspan=0><input type="submit" name="submit" 
    value="Register"></th></tr> </table>
    
     </form>
    
    
     <?php
     }
     ?>
    
  4. To delete a record you need some way of identifying that record. You could use the name of the contact but people do not have unique names. There could be multiple people called chris stored in your table and so any one with the same name will be deleted from your table.

     

    Instead what you should do is alter your contactme table and add an id field which is set to auto increment. Doing this will ensure each row has a unique id when a new record is inserted into the table. It is this is id you'd use to identify each record. To do this you can run this sql query

    ALTER TABLE  `contactme` ADD  `id` INT NOT NULL AUTO_INCREMENT FIRST,
    ADD PRIMARY KEY (`id`);
    

    Now if you look at the data in your table you'd should see each row now has a unique number assigned to them.

     

    Now that we have the id column we can use this to create the delete link. To do so in view-contactme.php change this line

    echo $rows['name'] .  " "  .$rows['email'] .  " " .  $rows['phoneno']  .  " " .  $rows['comments'] . "<br>";
    

    to

    echo $rows['name'] .  " "  .$rows['email'] .  " " .  $rows['phoneno']  .  " " .  $rows['comments'] . "| <a href=\"delete-contact.php?id={$row['id']}\">Delete</a><br>"; // added delete link. The record id is being passed in the url
    

    You should now see a delete link next to each row. Clicking the link will pass that rows id to delete-contact.php in the url

     

     

    In delete-contact.php you'd use  $_GET['id']  to retrieve the record id being passed. You'd run a query to delete the record where the id matches $_GET['id']. Example code using mysqli prepared query

    // connect to mysql using MySQLi
    $mysqli = new mysqli($host, $username, $password, $db_name);
    
    // check to make sure the id exists and it is a number
    if(isset($_GET['id']) && ctype_digit($_GET['id']))
    {
        // prepare the delete query
        $stmt = $mysqli->prepare("DELETE FROM $tbl_name WHERE id= ?");
    
        // Issue an error if the prepared statement failed
    
        if(!$stmt)
        {
    trigger_error('Unable to prepare query: ' . $mysqli->error);
        }

    // bind the record id to the query

    $stmt->bind_param('i', intval($_GET['id']));

     

    // execute the prepared query

    $result = $stmt->execute();

     

    // if the query did not execute trigger an error message

    if(!$result)

    {

    trigger_error("Unable to delete record #{$_GET['id']} from $tbl_name - " . $mysqli->error);

    }

     

    // check to make sure the query did affect the table.

    if($result && $mysqli->affected_rows)

    {

    echo "Record deleted successfully";

    }

    }

     

    Now because we added the id field to the table we need to go back and alter the code slightly in add-contact.php. Find the prepared query in add-contact.php and change it to this

    // specify the columns we are inserting the data into
    $stmt = $mysqli->prepare("INSERT INTO $tbl_name (name, email, phone, comment) VALUES (?, ?, ?, ?)");
    

    Now your challenge is to alter the code in view-contact.php over to mysqli. As I said earlier the use of the mysql_ functions are deprecated meaning they are no longer supported and could be removed from future versions of PHP.

    Notice: Undefined variable: row in C:\xampp\htdocs\hometown\hometown\view-contactme.php on line 14

    I now get this error do I need to add on line 14 id into arrary?

    <?php
    $host= "localhost";
    $user = "root";
    $passwd = "";
    $database = "test";
    $tbl_name = "contactme";
    
    mysql_connect ($host, $user, $passwd) or  die("cannot connect server ");
    mysql_select_db( $database) or die("cannot select DB");
    
    $result=mysql_query("SELECT * FROM $tbl_name");
    
    while($rows = mysql_fetch_array($result)){
    echo $rows['name'] .  " "  .$rows['email'] .  " " .  $rows['phoneno']  .  " " .  $rows['comments'] . "| <a href=\"delete-contact.php?id={$row['id']}\">Delete</a><br>"; // added delete link. The record id is being passed in the url
    }
    mysql_close();     //close database
    ?>
    
  5. Those errors you are getting are these. Correct?

    They are all caused by the first error. I have highlighted the cause of that error in red.

     

    You are getting that error because PHP has connected to MySQL but it has not selected a database. The code was not unable to initiate the prepare statement because no database has been selected.

     

    This is why I said you need to fill in your database credentials for the five variables I showed you in post #29. You understand what I mean by that?

    Thank you for your help. Do you any suggestion on how you can delete a entire row from view-contactme.php?

    <?php
    $host= "localhost";
    $user = "root";
    $passwd = "";
    $database = "test";
    $tbl_name = "contactme";
    
    mysql_connect ($host, $user, $passwd) or  die("cannot connect server ");
    mysql_select_db( $database) or die("cannot select DB");
    
    $result=mysql_query("SELECT * FROM $tbl_name");
    
    while($rows = mysql_fetch_array($result)){
    echo $rows['name'] .  " "  .$rows['email'] .  " " .  $rows['phoneno']  .  " " .  $rows['comments'] . "<br>";
    }
    mysql_close();     //close database
    ?>

    So basically I was thinking something like. Link to a new php called delete.php and have a submit button on the bottom of the view-contactme.php. Example of delete is below.

    <?php
    $host="localhost";         // Host name
    $username="root";         // Mysql username
    $password="";         // Mysql password
    $db_name="test";         // Database name
    $tbl_name="contactme";     // Table name
    
    mysql_connect ("$host", "$username", "$password") or die ("cannot connect server ");
    mysql_select_db ("$db_name") or die ("cannot select DB");
    
    // sql to delete a record
    $sql = "DELETE FROM guestbook WHERE name='chris'";
    
    if ($conn->query($sql) === TRUE) {
        echo "Record deleted successfully";
    } else {
        echo "Error deleting record: " . $conn->error;
    }
    
    $conn->close();
    ?>
  6. What? I don't understand what you just said.

     

    Sorry I have put the code in that you previously gave me but get two errors. As stated above not sure what is wrong.  I have the connection to the database from the start.

  7.  

    You have not connected to your database. Make sure you have filled in your database credentials on these four lines at the top of your script

    $host="localhost";         // Host name
    $username="root";         // Mysql username
    $password="";         // Mysql password
    $db_name="";         // Database name
    $tbl_name="contactme";     // Table name
    

    I have filled that had that previously i only took a small chunk of code for that example.

    Full code below.

    <?php
    
    $host="localhost";         // Host name
    $username="root";         // Mysql username
    $password="";         // Mysql password
    $db_name="";         // Database name
    $tbl_name="contactme";     // Table name
    
    // using mysqli see http://php.net/mysqli for documentation
    $mysqli = new mysqli("$host", "$username", "$password", "$db_name");
    
    // if a post request has been made
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        // Simple data validation checking to make sure name, email, phone and comment exist.
    
        // You should do futher checks here, ie making sure
        //      - a name has been entered - it is not just an empty string
        //      - email address entered is a valid address
        //      - the phone number is a valid phone number
        //      - a comment was entered - it is not just an empty string
    
            // set to true for now. This will be set to false if the data does not validate
            $isValidData = true;
    
            // used to log errors for fields that do not validate
            $errors = array();
    
            // check to make sure name exists in POST
    if(isset($_POST['author']))
    {
    $name = $_POST['author'];
    }
    else
    {
    // add error to $errors array
    $errors[] = 'Please enter your name';
    }
    
    // check to make sure email exists in POST
    if(isset($_POST['email']))
    {
    $email = $_POST['email'];
    }
    else
    {
    // add error to $errors array
    $errors[] = 'Please enter your email address';
    }
    
    // check to make sure phone exists in POST
    if(isset($_POST['phone']))
    {
    $phone = $_POST['phone'];
    }
    else
    {
    // add error to $errors array
    $errors[] = 'Please enter your phone number';
    }
    
    // check to make sure comment exists in POST
    if(isset($_POST['text']))
    {
    $comment = $_POST['text'];
    }
    else
    {
    // add error to $errors array
    $errors[] = 'Please enter your comment';
    }
    // end validation
        // check to make sure data did validate - nothing  should be in $errors.
        if(count($errors) != 0)
        {
            // set to false
            $isValidData = false;
    
            // display the error messages for fields that do not validate
            echo 'Please correct the following: <ul><li>' . implode('</li></li>', $errors) . '</li></ul>';
    
            // dispay the contact form again here
            include 'contact.html';
            exit;
        }
    
        // only run the query if the data is valid
        if($isValidData)
        {
            // use a prepared statement for inserting data into the table
            $stmt = $mysqli->prepare("INSERT INTO $tbl_name VALUES (?, ?, ?, ?)");
        if(!$stmt)
        {
         trigger_error('Unable to prepare query: ' . $mysqli->error);
        }
            // bind the values to the query
            $stmt->bind_param('ssss', $name,
                                      $email,
                                      $phone,
                                      $comment);
    
            // execute prepared statement
            $result = $stmt->execute();
    
            // make sure the statement did not return an error
            if(!$result)
            {
                // trigger error.
                trigger_error("Unable to insert data into $tbl_name in the database. " . $mysqli->error);
            }
    
            // check to see if the insert query did affect the table.
            if($result && $mysqli->affected_rows)
            {
                echo "Successful<BR>";
                echo "<a href='view-contactme.php'>View Contact Me</a>"; // link to view contact me page
            }
        }
    }
  8. where about do you want me to put this line in same if statement for example. 
        // only run the query if the data is valid
        if($isValidData)
        {
            // use a prepared statement for inserting data into the table
            $stmt = $mysqli->prepare("INSERT INTO $tbl_name VALUES (?, ?, ?, ?)");
        if(!$stmt)
        {
         trigger_error('Unable to prepare query: ' . $mysqli->error);
        }
            // bind the values to the query
            $stmt->bind_param('ssss', $name,
                                      $email,
                                      $phone,
                                      $comment);

    I get these errors using the above

    Notice: Unable to prepare query: No database selected in C:\xampp\htdocs\hometown\hometown\add-contactme.php on line 94 - trigger_error('Unable to prepare query: ' . $mysqli->error);

     

    Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\hometown\hometown\add-contactme.php on line 97 $stmt->bind_param('ssss', $name,

     

    Those are the two errors.

  9. Seems like the prepared query is failing.

     

    Before line 94 add the following

    if(!$stmt)
    {
         trigger_error('Unable to prepare query: ' . $mysqli->error);
    }
    

    What error is shown this time?

    where about do you want me to put this line in same if statement for example. 
        // only run the query if the data is valid
        if($isValidData)
        {
            // use a prepared statement for inserting data into the table
            $stmt = $mysqli->prepare("INSERT INTO $tbl_name VALUES (?, ?, ?, ?)");
        if(!$stmt)
        {
         trigger_error('Unable to prepare query: ' . $mysqli->error);
        }
            // bind the values to the query
            $stmt->bind_param('ssss', $name,
                                      $email,
                                      $phone,
                                      $comment);
  10. whats on line 94 in add-contactme.php

    line 94 - $stmt->bind_param('ssss', $name,

                                      $email,

                                      $phone,

                                      $comment);

  11. In your html your have named your Name field  author  and the Comment field  text  

     

    You are getting those validation messages because PHP is looking for fields named name   and   comment

     

    To correct the issue rename the fields in your HTML and PHP code so they match. The names you place in the square brackets for $_POST needs to match the name of the corresponding field in your HTML.

    Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\hometown\hometown\add-contactme.php on line 94

    I have no idea what this fatal error means can you explain?

  12. So the first block of code is contact.html?

     

    The second block of code is add-contact.php? If thats correct then this line in contact.html

     <form method="post" name="contact" action="#">
    

    should be

     <form method="post" name="contact" action="add-contact.php">
    

    .

    The code for add-contact.php should be like

    <?php
    
    $host="localhost";         // Host name
    $username="root";         // Mysql username
    $password="";         // Mysql password
    $db_name="";         // Database name
    $tbl_name="contactme";     // Table name
    
    // using mysqli see http://php.net/mysqli for documentation
    $mysqli = new mysqli("$host", "$username", "$password", "$db_name");
    
    // if a post request has been made
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        // Simple data validation checking to make sure name, email, phone and comment exist. 
    
        // You should do futher checks here, ie making sure 
        //      - a name has been entered - it is not just an empty string
        //      - email address entered is a valid address 
        //      - the phone number is a valid phone number
        //      - a comment was entered - it is not just an empty string
    
            // set to true for now. This will be set to false if the data does not validate
            $isValidData = true;
    
            // used to log errors for fields that do not validate
            $errors = array();
    
            // check to make sure name exists in POST
            if(isset($_POST['name']))
            {
                $name = $_POST['name'];
            }
            else
            {
                // add error to $errors array
                $errors[] = 'Please enter your name';
            }
    
            // check to make sure email exists in POST
            if(isset($_POST['email']))
            {
                $email = $_POST['email'];
            }
            else
            {
                // add error to $errors array
                $errors[] = 'Please enter your email address';
            }
    
            // check to make sure phone exists in POST
            if(isset($_POST['phone']))
            {
                $phone = $_POST['phone'];
            }
            else
            {
                // add error to $errors array
                $errors[] = 'Please enter your phone number';
            }
    
            // check to make sure comment exists in POST
            if(isset($_POST['comment']))
            {
                $comment = $_POST['comment'];
            }
            else
            {
                // add error to $errors array
                $errors[] = 'Please enter your comment';
            }
        // end validation
    
        // check to make sure data did validate - nothing  should be in $errors.
        if(count($errors) != 0)
        {
            // set to false
            $isValidData = false;
    
            // display the error messages for fields that do not validate
            echo 'Please correct the following: <ul><li>' . implode('</li></li>', $errors) . '</li></ul>';
    
            // dispay the contact form again here
            include 'contactme.html';
            exit;
        }
    
        // only run the query if the data is valid
        if($isValidData)
        {
            // use a prepared statement for inserting data into the table
            $stmt = $mysqli->prepare("INSERT INTO $tbl_name VALUES (?, ?, ?, ?)");
    
            // bind the values to the query
            $stmt->bind_param('ssss', $name, 
                                      $email, 
                                      $phone, 
                                      $comment);
    
            // execute prepared statement
            $result = $stmt->execute();
    
            // make sure the statement did not return an error
            if(!$result)
            {
                // trigger error.
                trigger_error("Unable to insert data into $tbl_name in the database. " . $mysqli->error);
            }
    
            // check to see if the insert query did affect the table.
            if($result && $mysqli->affected_rows)
            {
                echo "Successful<BR>";
                echo "<a href='view-contactme.php'>View Contact Me</a>"; // link to view contact me page
            }
        }
    }
    
    

     

    As you can see there is a lot more code involved. I have added comments to all lines which explains what is happening. The code will either insert the data into the database or display basic validation errors if the data does not validate. 

    All i get now is the following? Can you explain why? Please correct the following:

    • Please enter your name
    • Please enter your comment

    After adding a name, email, phoneno and comment it still comes up which that message why?

     

    I do understand why the if statement is their, but still don't understand why the error is occurring still. Unless its above the given char length which I doubt. I only put 'Chris' and 'Hello' in the comments.

  13. Ok Ignore my last reply. I though you were using command prompt to run your code. You just confirmed you are only using it to for accessing your mysql database.

     

    Can you post your current code you are using.

    <!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=utf-8" />
    <title>Contact Chris</title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <!-- templatemo 301 blue ice -->
    <!--
    Blue Ice Template
    http://www.templatemo.com/preview/templatemo_301_blue_ice
    -->
    <link href="templatemo_style.css" rel="stylesheet" type="text/css" />
    
    </head>
    <body>
    <?php echo $result; ?>
    
    <div id="templatemo_wrapper">
    
        <div id="templatemo_header">
          <p><!-- end of site_title -->
              
            </p>
            <ul id="social_box">
              <li><a href="https://www.facebook.com/christopher.deaton.399"><img src="images/facebook.png" alt="facebook" /></a></li>
                <li><a href="https://twitter.com/G3NERALCHRIS"><img src="images/twitter.png" alt="twitter" /></a></li>
                <li></li>
                <li></li>
                <li></li>                
            </ul>
            
        </div> <!-- end of templatemo_header -->
        
        <div id="templatemo_menu">
            <div class="home"><a href="index.html"></a></div>
            <ul>
              <li><a href="index.html">WHAT WE DO<span>Home</span></a></li>
              <li><a href="guestbook.php">Sign<span>Guestbook</span></a></li>
              <li><a href="about.html">WHO WE ARE<span>About Me</span></a></li>
              <li><a href="contact.html" class="last">ANY QUESTION?<span>Contact Me</span></a></li>
            </ul>
        </div> <!-- end of templatemo_menu -->
        
        <div id="templatemo_content_wrapper">
            <div id="templatemo_content_top"></div>
            <div id="templatemo_content">
            
                <h2>Contact Me!</h2>
    
                <p> </p>
                <div id="contact_form">
                
                  <h4>Quick Contact</h4>
                        
                        <form method="post" name="contact" action="#">
                            
                            <div class="col_w340 float_l">
                            
                                <label for="author">Name:</label> <input name="author" type="text" class="input_field" id="author" maxlength="40" />
                                  <div class="cleaner_h10"></div>
                                
                                <label for="email">Email:</label> <input name="email" type="text" class="input_field" id="email" maxlength="40" />
                                  <div class="cleaner_h10"></div>
                                
                                <label for="phone">Phone:</label> <input name="phone" type="text" class="input_field" id="phone" maxlength="40" />
                                <div class="cleaner_h10"></div>
                                
                            </div>
                            
                            <div class="col_w340 float_r">
                            
                                <label for="text">Message:</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea>
                                <div class="cleaner_h10"></div>
                                
                                <input name="submit" type="submit" class="submit_btn float_l" id="submit" formaction="add-contactme.php" value="Send" />
                                <input type="reset" class="submit_btn float_r" name="reset" id="reset" value="Reset" />
                                
                            </div>
                            
                       </form>
    
                </div>
                 <p> </p>
                <div class="cleaner"></div>
                
            </div>
            <div id="templatemo_content_bottom"></div>
        </div>
        <div id="templatemo_footer"></div> <!-- end of templatemo_footer -->
        
    </div> <!-- end of templatemo_wrapper -->    
    
    </body>
    </html>
    <?php
    $host="localhost";         // Host name
    $username="root";         // Mysql username
    $password="";         // Mysql password
    $db_name="test";         // Database name
    $tbl_name="contactme";     // Table name
    
    mysql_connect ("$host", "$username", "$password") or die ("cannot connect server ");
    mysql_select_db ("$db_name") or die ("cannot select DB");
    
    if (isset($_POST['author']) && ($_POST['author'] !='')){
    // get data from post
    $name = $_POST["author"];
    $email = $_POST["email"];
    $phoneno = $_POST["phone"];
    $comments = $_POST["text"];
    
    $sql="INSERT INTO $tbl_name VALUES ('$name', '$email', '$phoneno''$comments')";
    $result=mysql_query($sql);
    // echo result
    $result = $name - $email - $phoneno - $comments;
    }
    
    
    if($result){
    echo "Successful" . "<BR>";
    echo "<a href='view-contactme.php'>View Contact Me</a>"; // link to view contact me page
    }
    mysql_close();
    ?>

    This last is to view whether it works or not.

    <?php
    $host= "localhost";
    $user = "root";
    $passwd = "";
    $database = "test";
    $tbl_name = "contactme";
    
    mysql_connect ($host, $user, $passwd) or  die("cannot connect server ");
    mysql_select_db( $database) or die("cannot select DB");
    
    $result=mysql_query("SELECT * FROM $tbl_name");
    
    while($rows = mysql_fetch_array($result)){
    echo $rows['name'] .  " "  .$rows['email'] .  " " .  $rows['phoneno']  .  " " .  $rows['comments'] . "<br>";
    }
    mysql_close();     //close database
    ?>

     

     

  14. What? You are running your script from a desktop command window (bash terminal or cmd.exe) to test it? So you are not running it from your web browser?

     

    In order for your script to add anything to the database you need run a POST HTTP request to your PHP script passing in the data for the fields named author, email, phone and text. Otherwise no data will be added to your database!

    I'm using xampp to run mysql and then running command prompt to see if any data has been added to the table. Can you explain how POST HTTP works and how I would go about doing this for my work please?

  15. looking at the HTML code you have posted your not getting the values because your asking php to collect Name, Email, Phoneno, Comments but your id / names of your inputs are Authour, Email, Phone, Text. the $_POST will be looking for the input id / name to get the post from.

    if (isset($_POST['author']) && ($_POST['author'] !='')){
    // get data from post
    $name = $_POST["author"];
    $email = $_POST["email"];
    $phoneno = $_POST["phone"];
    $comments = $_POST["text"]; 
    
    // echo result
    $result = $name - $email - $phoneno - $comments;
    }

    just put the <?php echo $result; ?> between your body tags in the HTML for testing purposes and it should display what is being sent from the form.

     

    This does work but I put in my code like so, in  my add-contactme.php

    and added that line in my html. But using command prompt it hasn't added nothing into the database itself but i does come up like so

    echo "Successful" . "<BR>"; so this show Succesful when it work this show but nothing has been added to the table. Any suggestions?

    if (isset($_POST['author']) && ($_POST['author'] !='')){
    // get data from post
    $name = $_POST["author"];
    $email = $_POST["email"];
    $phoneno = $_POST["phone"];
    $comments = $_POST["text"];
    
    $sql="INSERT INTO $tbl_name VALUES ('$name', '$email', '$phoneno''$comments')";
    $result=mysql_query($sql);
    // echo result
    $result = $name - $email - $phoneno - $comments;
    }
  16. Your form seems to work fine.  Try adding <?php echo('<pre>'.print_r($_POST,1).'</pre>'); ?> right below the opening <body> tag, and you will see your post values.  Note that I never used the attribute formaction before, however, it seems to override the form to go to that URL (however, it doesn't validate?). Either get rid of this attribute and let your form tag define the location, or name this main file "add-contactme.php"

     This did not work any other suggestion would be helpful. From that you told me to add <?php echo('<pre>'.print_r($_POST,1).'</pre>'); ?>

    below the body in the contact.html. Unless something is going wrong. I can't not explain basically this is my second sql table and I basically used the same method but instead of hyperlinking from text in each html i have a contact section which saves the contacts in the table. Unlike my comments one which just save comments and is viewable. 

    That is what I did below for guestbook. This works simple enough. But my contact page is more complicated. Unless I can create inside the html that php any suggestion.

    <html>
    <style type="text/css">
    @import url("templatemo_style.css");
    </style>
    <body>My Hometown Guestbook </p>
    
    <form action="add-guestbook.php" method="post" style="font-size: 36px">
        Name: <input type = "text" name="name"><br>
        E-mail: <input type = "text" name="email"><br>
        Comments: <input type = "text" name="comments"><br>
        <input type="submit">
    
    </form>
    </body>
    </html>
  17. My html code. For contact.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Contact Chris</title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <!-- templatemo 301 blue ice -->
    <!-- 
    Blue Ice Template 
    http://www.templatemo.com/preview/templatemo_301_blue_ice 
    -->
    <link href="templatemo_style.css" rel="stylesheet" type="text/css" />
    
    </head>
    <body>
    
    <div id="templatemo_wrapper"> 
    
    	<div id="templatemo_header">
    	  <p><!-- end of site_title -->
        	  
      	  </p>
        	<ul id="social_box">
              <li><a href="https://www.facebook.com/christopher.deaton.399"><img src="images/facebook.png" alt="facebook" /></a></li>
                <li><a href="https://twitter.com/G3NERALCHRIS"><img src="images/twitter.png" alt="twitter" /></a></li>
                <li></li>
                <li></li>
                <li></li>                
            </ul>
            
        </div> <!-- end of templatemo_header -->
        
        <div id="templatemo_menu">
        	<div class="home"><a href="index.html"></a></div>
        	<ul>
        	  <li><a href="index.html">WHAT WE DO<span>Home</span></a></li>
        	  <li><a href="guestbook.php">Sign<span>Guestbook</span></a></li>
        	  <li><a href="about.html">WHO WE ARE<span>About Me</span></a></li>
        	  <li><a href="contact.html" class="last">ANY QUESTION?<span>Contact Me</span></a></li>
      	  </ul>
        </div> <!-- end of templatemo_menu -->
        
        <div id="templatemo_content_wrapper">
        	<div id="templatemo_content_top"></div>
            <div id="templatemo_content">
            
                <h2>Contact Me!</h2>
    
                <p> </p>
                <div id="contact_form">
                
                  <h4>Quick Contact</h4>
                        
                        <form method="post" name="contact" action="#">
                            
                            <div class="col_w340 float_l">
                            
                                <label for="author">Name:</label> <input name="author" type="text" class="input_field" id="author" maxlength="40" />
                              	<div class="cleaner_h10"></div>
                                
                                <label for="email">Email:</label> <input name="email" type="text" class="input_field" id="email" maxlength="40" />
                              	<div class="cleaner_h10"></div>
                                
                                <label for="phone">Phone:</label> <input name="phone" type="text" class="input_field" id="phone" maxlength="40" />
                                <div class="cleaner_h10"></div>
                                
                            </div>
                            
                            <div class="col_w340 float_r">
                            
                                <label for="text">Message:</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea>
                                <div class="cleaner_h10"></div>
                                
                                <input name="submit" type="submit" class="submit_btn float_l" id="submit" formaction="add-contactme.php" value="Send" />
                                <input type="reset" class="submit_btn float_r" name="reset" id="reset" value="Reset" />
                                
                            </div>
                            
                       </form>
    
                </div>
           	  <p> </p>
                <div class="cleaner"></div>
                
            </div>
            <div id="templatemo_content_bottom"></div>
    	</div>
        <div id="templatemo_footer"></div> <!-- end of templatemo_footer -->
        
    </div> <!-- end of templatemo_wrapper -->    
    
    </body>
    </html>
    
    
    
  18.  

    At the top of your script, add the following.  This will print out all the fields you are sending in the form to the server.  What do you see?  If $_POST["phoneno"], etc are not set, you will get this error notice.  So, your issue has nothing to do with the script you are showing, but your HTML form.  Make sure the inputs have names, and always use print_r or var_dump as a reality check.

    echo('<pre>'.print_r($_POST,1).'</pre>');

    I literally have no clue what that means? Can you explain where I could put this line to make it useful in my code.

  19. I am creating a website for university coursework.

    <input name="submit" type="submit" class="submit_btn float_l" id="submit" formaction="add-contactme.php" value="Send" />

    The above code is from my html for contact.html if that is correct. I am using a form and then a submit button.

    I keep getting

    Notice: Undefined index: name in C:\xampp\htdocs\hometown\hometown\add-contactme.php on line 11

    Notice: Undefined index: email in C:\xampp\htdocs\hometown\hometown\add-contactme.php on line 12

    Notice: Undefined index: phoneno in C:\xampp\htdocs\hometown\hometown\add-contactme.php on line 13

    Notice: Undefined index: comments in C:\xampp\htdocs\hometown\hometown\add-contactme.php on line 14

     

    Also if anyone could help me I would like to create a delete button after a person has added the values into the sql table, basically to the last row inputted into the database. I want to avoid using delete from contactme where name ='chris';

    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password=""; // Mysql password
    $db_name="test"; // Database name
    $tbl_name="contactme"; // Table name
    
    mysql_connect ("$host", "$username", "$password") or die ("cannot connect server ");
    mysql_select_db ("$db_name") or die ("cannot select DB");
    
    $name = $_POST["name"];
    $email = $_POST["email"];
    $phoneno = $_POST["phoneno"];
    $comments = $_POST["comments"];
    
    $sql="INSERT INTO $tbl_name VALUES ('$name', '$email', '$phoneno''$comments')";
    $result=mysql_query($sql);
    
    if($result){
    echo "Successful" . "
    ";
    echo "view-contactme.php"; // link to view contact me page
    }
    mysql_close();
    ?>
×
×
  • 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.