PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Jun 24, 2019 9:38 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Wed May 04, 2016 9:21 am 
Offline
Forum Newbie

Joined: Wed May 04, 2016 6:46 am
Posts: 12
Celauran wrote:
Date is a string. That needs to be 'ssisi'

sory, i've tryed to find a solution and changed it for s,i,d... but stil getting value:0


Top
 Profile  
 
PostPosted: Wed May 04, 2016 9:23 am 
Offline
Forum Newbie

Joined: Wed May 04, 2016 6:46 am
Posts: 12
if i change it on 's' that returns Incorrect datetime value: '' for column 'date' at row 1 that error


Top
 Profile  
 
PostPosted: Wed May 04, 2016 9:34 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
And that's on edit rather than create?

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed May 04, 2016 9:48 am 
Offline
Forum Newbie

Joined: Wed May 04, 2016 6:46 am
Posts: 12
i changed 'i' on 's' in add section and that works fine. now i can add records!
thank you a lot, sir!
but stil having the problem with updating(((


Top
 Profile  
 
PostPosted: Wed May 04, 2016 1:17 pm 
Offline
Forum Newbie

Joined: Wed May 04, 2016 6:46 am
Posts: 12
i dont want to be officious, but could smone help me to find any solution for my editing form problem? I realy want to find my mistake...


Top
 Profile  
 
PostPosted: Wed May 04, 2016 5:56 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
I've actually got some time to look at this again. How far did you get? What's your most recent code?

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed May 04, 2016 6:32 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Quote:
Syntax: [ Download ] [ Hide ]
 if($name == '' || $nummer == '' || $cdate = '' || $stuck == ''){

There's the problem. $cdate = '' should be $cdate == ''

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Thu May 05, 2016 7:17 am 
Offline
Forum Newbie

Joined: Wed May 04, 2016 6:46 am
Posts: 12
Well sir, you are genius!
That solved my problem. Thank you so much for your time and nice nswers!
Here is the final working code, maybe it will help someone.

Syntax: [ Download ] [ Hide ]
<?php
//connect
include ("db/connect.php");

//create new/edit

function renderForm($nameS = '', $nummerS = '', $dateS='', $stuckS='', $error='',$id=''){
        $cdate = date(' Y-m-d H:i:s');
        ?>
        <!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>Name: </strong> <input type="text" name="name" value="<?php echo $nameS; ?>"/><br/>
                        <strong>Nummer: </strong> <input type="text" name="nummer" value="<?php echo $nummerS; ?>"/><br/>
                        <strong>Date: </strong> <input type="text" name="date" value="<?php echo $cdate; ?>"/><br/>
                        <strong>Stuck: </strong> <input type="text" name="stuck" value="<?php echo $stuckS; ?>"/><br/>
                        <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'];
$name = htmlentities($_POST['name'], ENT_QUOTES);
$nummer = htmlentities($_POST['nummer'], ENT_QUOTES);
$stuck = htmlentities($_POST['stuck'], ENT_QUOTES);
$cdate = date('Y-m-d H:i:s');

//check if empty
if($name == '' || $nummer == '' || $cdate == '' || $stuck == ''){
        $error = 'ERROR: Please fill in all required fields!';
        renderForm($name, $nummer, $cdate, $stuck, $error, $id);
}else{
        // if everything is fine, update the record in the database
        if($stmt = $mysqli->prepare("UPDATE ware SET name = ?, nummer = ?, date = ?, stuck = ? WHERE id=?")){
                $stmt->bind_param("sssii",$name,$nummer, $cdate,  $stuck, $id);

                if(!$stmt->execute()) echo $stmt->error;
                /*if($stmt->execute()){
                        // it worked
                        echo "DONE!";
                }else {
                // it didn't
                        echo "ERROR!";
                }*/


                $stmt->close();
        }else{
                //show error
                echo "ERROR: could not prepare SQL statement.";

        }
        //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 recod from the database
if($stmt = $mysqli->prepare("SELECT * FROM ware WHERE id=?"))
{
        $stmt->bind_param("i",$id);
        $stmt->execute();

        $stmt->bind_result($id,$name, $nummer, $cdate, $stuck);
        $stmt->fetch();

        //show form
        renderForm($name, $nummer, $cdate, $stuck, 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
$name = htmlentities($_POST['name'], ENT_QUOTES);
$nummer = htmlentities($_POST['nummer'], ENT_QUOTES);
$stuck = htmlentities($_POST['stuck'], ENT_QUOTES);
        $cdate = date(' Y-m-d H:i:s');


// check that are both not empty
if ($name == '' || $nummer == '' || $stuck == '' || $cdate =='')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($name, $nummer, $cdate, $stuck, $error);
}
else
{
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT ware (name, nummer, date, stuck) VALUES (?, ?,?,?)"))
{
$stmt->bind_param("sssi", $name, $nummer,$cdate, $stuck);
if(!$stmt->execute()) echo $stmt->error;
$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.php");
}

}
// if the form hasn't been submitted yet, show the form
else
{
renderForm();
}
}
// close the mysqli connection
$mysqli->close();
?>


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group