Jump to content

PHP mysql date help


Go to solution Solved by Ch0cu3r,

Recommended Posts

:suicide:  This is me at this point.. not really but I do need some help, Im trying to complete a project, it was supposed to be a group project but with only 6 people in class we opted to try to figure it out on our own.. anyway.. I cannot get my Start/End DATE field to update or insert? 

http://www.ctcsports.org/upload/Fall2013/CIST2352/900104329/Assignment33/view_contracts.php

my file to create the table

// Create a MySQL table in the selected database
$sql = "DROP TABLE IF EXISTS dRcontract";
mysql_query($sql);
mysql_query("CREATE TABLE `dRcontract` (
 `cid` int(11) NOT NULL auto_increment,
 `pid` int(11) NOT NULL,
 `tid` int(11)  NOT NULL,
 `Nickname` VARCHAR(50),
  `StartDate` DATE,
  `EndDate` DATE,
  `PlayerSalary` decimal,
 
 PRIMARY KEY (`cid`),
 UNIQUE (`pid`),
 UNIQUE (`tid`)

)")
or die(mysql_error());  

echo "Table Created!";

?> 
 

Here is the contract_model.php file

<?php
        /*
                Allows the user to both create new model and edit existing model
        */

        // connect to the database
        include("connect-db.php");
        
					
				
				
					
					//PlayerSalary decimal

        // 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($pid = '', $tid ='', $Nickname = '', $StartDate = '',$EndDate = '', $PlayerSalary = '', $error = '', $cid = '')
        { ?>
                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
                <html>
                        <head>  
                        	
                                <title>
                                        <?php if ($cid != '') { echo "Edit Record"; } else { echo "New Record"; } ?>
                                </title>
                                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                                <link rel="stylesheet" type="text/css" href="Styles.css" />
                        </head>
                        <body>
                                <h1><?php if ($cid != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1>
                                <?php if ($error != '') {
                                        echo "<div style='padding:4px; border:1px solid pink; color:pink'>" . $error
                                                . "</div>";
                                } ?>
                                
                                <form action="" method="post">
                                <div>
                                        <?php if ($cid != '') { ?>
                                                <input type="hidden" name="cid" value="<?php echo $cid; ?>" />
                                                <p>ID: <?php echo $cid; ?></p>
                                        <?php } ?>
                                        
                                        
                                      <strong>Player ID: *</strong><input type="text" name="pid"
                                                value="<?php echo $pid; ?>"/><br/>
                                        <strong>Team Id: *</strong> <input type="text" name="tid"
                                                value="<?php echo $tid; ?>"/><br/>
                                        <strong>Nickname: *</strong> <input type="text" name="Nickname"
                                                value="<?php echo $Nickname; ?>"/><br/>
                                         <strong>StartDate: </strong> <input type="text" name="StartDate"
                                                value="<?php echo $StartDate; ?>"/><br/>
                                        <strong>EndDate: </strong> <input type="text" name="EndDate"
                                                value="<?php echo $EndDate; ?>"/><br/>
                                        <strong>PlayerSalary: </strong> <input type="text" name="PlayerSalary"
                                                value="<?php echo $PlayerSalary; ?>"/><br/>
                                        <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['cid']))
        {
                // 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['cid']))
                        {
                                // get variables from the URL/form
                                //ENT_QUOTES - Decodes double and single quotes
                                $cid = $_POST['cid'];
                                $pid = htmlentities($_POST['pid'], ENT_QUOTES);
                                $tid = htmlentities($_POST['tid'], ENT_QUOTES);
                                $Nickname = htmlentities($_POST['Nickname'], ENT_QUOTES);
                                $StartDate = htmlentities($_POST['StartDate'], ENT_QUOTES);
                                $EndDate = htmlentities($_POST['EndDate'], ENT_QUOTES);
                                $PlayerSalary = htmlentities($_POST['PlayerSalary'], ENT_QUOTES);
                                
                                // check that pidname and addressname are both not empty
                                if ($pid == '' || $tid == '')
                                {
                                        // if they are empty, show an error message and display the form
                                        $error = 'ERROR: Please fill in all required fields!';
                                        renderForm($pid, $tid,$Nickname, $StartDate, $EndDate,$PlayerSalary, $error, $id);
                                }
                                else
                                {
                                        // if everything is fine, update the record in the database
                                        // bind statement string, string, string, integer
                                        if ($stmt = $mysqli->prepare("UPDATE dRcontract SET pid = ?, tid = ?, Nickname = ?, StartDate = ? ,EndDate = ?,PlayerSalary = ?
                                                WHERE cid=?"))
                                        {
                                                $stmt->bind_param("iisiiii", $pid, $tid, $Nickname, $StartDate, $EndDate, $PlayerSalary, $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_contracts.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['cid']) && $_GET['cid'] > 0)
                        {
                                // get 'id' from URL
                                $cid = $_GET['cid'];
                                
                                // get the recod from the database
                                if($stmt = $mysqli->prepare("SELECT * FROM dRcontract WHERE cid=?"))
                                {
                                        $stmt->bind_param("i", $cid);
                                        $stmt->execute();
                                        
                                        $stmt->bind_result($cid, $pid, $tid,$Nickname, $StartDate, $EndDate, $PlayerSalary);
                                        $stmt->fetch();
                                        
                                        // show the form
                                        renderForm($pid, $tid, $Nickname, $StartDate, $EndDate, $PlayerSalary, NULL, $cid);
                                        
                                        $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_contracts.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
                        $pid = htmlentities($_POST['pid'], ENT_QUOTES);
                        $tid = htmlentities($_POST['tid'], ENT_QUOTES);
                        $Nickname = htmlentities($_POST['Nickname'], ENT_QUOTES);
                        $StartDate = htmlentities($_POST['StartDate'], ENT_QUOTES);
                        $EndDate = htmlentities($_POST['EndDate'], ENT_QUOTES);
                        $PlayerSalary = htmlentities($_POST['PlayerSalary'], ENT_QUOTES);
                        
                        // check that pidname and addressname are both not empty
                        if ($pid == '' || $tid == '')
                        {
                                // if they are empty, show an error message and display the form
                                $error = 'ERROR: Please fill in all required fields!';
                                renderForm($pid, $tid, $Nickname, $StartDate, $EndDate, $PlayerSalary, $error);
                        }
                        else
                        {
                                // insert the new record into the database
                                if ($stmt = $mysqli->prepare("INSERT dRcontract (pid, tid, Nickname, StartDate, EndDate,PlayerSalary) VALUES (?, ?, ?, ? ,?, ?)"))
                                {
                                        $stmt->bind_param("iisiii", $pid, $tid,$Nickname, $StartDate, $EndDate, $PlayerSalary);
                                        $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: view_contracts.php");
                        }
                        
                }
                // if the form hasn't been submitted yet, show the form
                else
                {
                        renderForm();
                }
        }
        
        // close the mysqli connection
        $mysqli->close();
?> 



Link to comment
https://forums.phpfreaks.com/topic/283187-php-mysql-date-help/
Share on other sites

if ($stmt = $mysqli->prepare("INSERT dRcontract (pid, tid, Nickname, StartDate, EndDate,PlayerSalary) VALUES (?, ?, ?, ? ,?, ?)"))

Should be: 

if ($stmt = $mysqli->prepare("INSERT INTO dRcontract (pid, tid, Nickname, StartDate, EndDate,PlayerSalary) VALUES (?, ?, ?, ? ,?, ?)"))

I would suggest as cyber says and output the error to find out exactly where it is failing.

Edited by Strychnine
Link to comment
https://forums.phpfreaks.com/topic/283187-php-mysql-date-help/#findComment-1454936
Share on other sites

you are doing no check at all that the values being passed in for start and end dates are in the expected format. You need to make sure that the dates are in the Y-m-d format. So 5th jan 2013 would be 2013-01-05. If you dont pass a correctly formatted date into the query it will not insert

Link to comment
https://forums.phpfreaks.com/topic/283187-php-mysql-date-help/#findComment-1454937
Share on other sites

  • Solution

The bind values for the $StartDate and $EndDate, I think need to be s not i

// bind params for update
$stmt->bind_param("iisssii", $pid, $tid, $Nickname, $StartDate, $EndDate, $PlayerSalary, $id);

// bind params for new record
$stmt->bind_param("iisssi", $pid, $tid,$Nickname, $StartDate, $EndDate, $PlayerSalary);
Edited by Ch0cu3r
Link to comment
https://forums.phpfreaks.com/topic/283187-php-mysql-date-help/#findComment-1454938
Share on other sites

Thanks so much. Like I said above I have some household things to do but I will be back in a little bit, as you can see I have many issues with my program.. I really look up to those of you that have learned this so well,… I hope to be of some help in the future.. :-)

Link to comment
https://forums.phpfreaks.com/topic/283187-php-mysql-date-help/#findComment-1454941
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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