Jump to content

Values not going in DB a Further Step


chrisguk

Recommended Posts

Okay,

 

I have moved on a few more steps and now starting to add, edit and delete records.  The first issues I encountered before have been fixed as I read up on the prepare statement.  I had to rewrite most of the code though.

 

With this issue I am having trouble get the script to update or add new records.  It produces no error messages:

 

My DB:

 

Column   Type         Null Default

id           int(11)         No Autoincrement

page           varchar(255) Yes NULL

title           varchar(255) Yes NULL

description  varchar(255) Yes NULL

keywords   varchar(255) Yes NULL

 

The code:

view.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
        <head>  
                <title>View Records</title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        </head>
        <body>
                
                <h1>View Records</h1>
                
                <p><b>View All</b> | <a href="paginated.php">View Paginated</a></p>
                
                <?php
                        // connect to the database
                        include('connect.php');
                        
                        // get the records from the database
                        if ($result = $mysqli->query("SELECT * FROM URL ORDER BY id"))
                        {
                                // display records if there are records to display
                                if ($result->num_rows > 0)
                                {
                                        // display records in a table
                                        echo "<table border='1' cellpadding='10'>";
                                        
                                        // set table headers
                                        echo "
                                        <tr>
                                        <th>ID</th>
                                        <th>Page</th>
																			<th>Title</th>
																			<th>Description</th>
																			<th>Keywords</th>
																			<th>Edit</th>
																			<th>Delete</th></tr></tr>";
                                        
                                        while ($row = $result->fetch_object())
                                        {
                                                // set up a row for each record
                                                echo "<tr>";
                                                echo "<td>" . $row->id . "</td>";
                                                echo "<td>" . $row->page . "</td>";
                                                echo "<td>" . $row->title . "</td>";
                                                echo "<td>" . $row->description . "</td>";
                                                echo "<td>" . $row->keywords . "</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>";
                                }
                                // if there are no records in the database, display an alert message
                                else
                                {
                                        echo "No results to display!";
                                }
                        }
                        // show an error if there is an issue with the database query
                        else
                        {
                                echo "Error: " . $mysqli->error;
                        }
                        
                        // close database connection
                        $mysqli->close();
                
                ?>
                
                <a href="records.php">Add New Record</a>
        </body>
</html>

 

connect.php

// connect to the database
$mysqli = new mysqli($server, $user, $pass, $db);

// show errors
mysqli_report(MYSQLI_REPORT_ERROR);

Link to comment
https://forums.phpfreaks.com/topic/261495-values-not-going-in-db-a-further-step/
Share on other sites

The code you posted has nothing to do with insert or update

 

Jes Sorry I posted the wrong extract:

 

<?php
        // connect to the database
        include("connect.php");

        // creates the new/edit record form
        // A function that is easily reusable
        function renderForm($page = '', $title = '', $description = '', $keywords = '', $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 "New Record"; } ?>
                                </title>
                                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                        </head>
                        <body>
                                <h1><?php if ($id != '') { echo "Edit Record"; } else { echo "New 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 } ?>
                                        <strong>Page: *</strong> <input type="text" name="page"
                                                value="<?php echo $page; ?>"/><br/>
                                        <strong>Title: *</strong> <input type="text" name="title"
                                                value="<?php echo $title; ?>"/><br/>
                                        <strong>Description: *</strong> <input type="text" name="description"
                                                value="<?php echo $description; ?>"/><br/>
                                        <strong>Keywords: *</strong> <input type="text" name="keywords"
                                                value="<?php echo $keywords; ?>"/>
                                        <p>* required</p>
                                        <input type="submit" name="submit" value="Submit" />
                                </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'];
                                $page = htmlentities($_POST['page'], ENT_QUOTES);
                                $title = htmlentities($_POST['title'], ENT_QUOTES);
                                $description = htmlentities($_POST['description'], ENT_QUOTES);
                                $keywords = htmlentities($_POST['keywords'], ENT_QUOTES);
                                
                                // check that page and title are both not empty
                                if ($page == '' || $title == '')
                                {
                                        // if they are empty, show an error message and display the form
                                        $error = 'ERROR: Please fill in all required fields!';
                                        renderForm($page, $title, $description, $keywords, $error, $id);
                                }
                                else
                                {
                                        // if everything is fine, update the record in the database
                                        if ($stmt = $mysqli->prepare("UPDATE URL SET page = ?, title = ?, description = ?, keywords = ?
                                                WHERE id=?"))
                                        {
                                                $stmt->bind_param("ssi", $page, $title, $description, $keywords, $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: 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 record from the database
                                if($stmt = $mysqli->prepare("SELECT * FROM URL WHERE id=?"))
                                {
                                        $stmt->bind_param("i", $id);
                                        $stmt->execute();
                                        
                                        $stmt->bind_result($id, $page, $title, $description, $keywords);
                                        $stmt->fetch();
                                        
                                        // show the form
                                        renderForm($page, $title, $description, $keywords, 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
                        $page = htmlentities($_POST['page'], ENT_QUOTES);
                        $title = htmlentities($_POST['title'], ENT_QUOTES);
                        $description = htmlentities($_POST['description'], ENT_QUOTES);
                        $keywords = htmlentities($_POST['keywords'], ENT_QUOTES);
                        
                        // check that page and title are both not empty
                        if ($page == '' || $title == '')
                        {
                                // if they are empty, show an error message and display the form
                                $error = 'ERROR: Please fill in all required fields!';
                                renderForm($page, $title,$description, $keywords, $error);
                        }
                        else
                        {
                                // insert the new record into the database
                                if ($stmt = $mysqli->prepare("INSERT URL (page, title, description, keywords) VALUES (?, ?)"))
                                {
                                        $stmt->bind_param("ss", $page, $title, $description, $keywords);
                                        $stmt->execute();
                                        $stmt->close();
                                }
                                // show an error if the query has an error
                                else
                                {
                                        echo "ERROR: Could not prepare SQL statement.";
                                }
                                
                                // redirect the user
                                header("Location: view.php");
                        }    
                }
                // if the form hasn't been submitted yet, show the form
                else
                {
                        renderForm();
                }
        }
        // close the mysqli connection
        $mysqli->close();
?>

You have so many if/else cases - if I encounter a situation like this, I like to add an echo in every single case, echoing where the code is, so when it dies I know where it ended at, including success cases like it did insert. Make sure to print_r your $_GET and $_POST and see that you have the data you expected.

Hi Jes,

 

I am having issues with this error which I believe is stopping the script:

 

array(5) { ["page"]=> string(6) "asdasd" ["title"]=> string(4) "rere" ["description"]=> string(5) "Test2" ["keywords"]=> string(23) "device helper structure" ["submit"]=> string(6) "Submit" }

Warning: mysqli::prepare() [mysqli.prepare]: (21S01/1136): Column count doesn't match value count at row 1 in /var/www/test/records.php on line 155

ERROR: Could not prepare SQL statement.

 

I just cant trace it


//LINE 155 ======== if ($stmt = $mysqli->prepare("INSERT URL (page, title, description, keywords) VALUES (?, ?)"))
                                {
                                        $stmt->bind_param("ss", $page, $title, $description, $keywords);
                                        $stmt->execute();
                                        $stmt->close();
                                }

Archived

This topic is now archived and is closed to further replies.

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