Frankice Posted January 10, 2020 Share Posted January 10, 2020 I'm facing database submission issues, please can someone with a good heart help me. Below is my code I don't know what I did wrong it won't submit mydata to the database it gives me error. Notice: Undefined variable: error in /home/psdelive/public_html/admin/add package.php on line 118 <?php include('../connection.php'); session_start(); if (isset($_POST['submit'])){ $date = $_POST['date']; $location = $_POST['location']; $time = $_POST['time']; $track = $_POST['track']; $activity = $_POST['activity']; $details = $_POST['details']; { $query = "INSERT INTO package(date,location,time,track_code,activity,details)VALUES('$date','$location','$time','$track','$activity','$details')"; $check = mysqli_query($dbConn,$query); if($check) { header('location:success_package'); } else { $error = '<div class="alert-warning" style="height:60px;"> <p style="line-height:40px;">Error in posting</p> </div>'; } } } $sele = "SELECT * from details"; $query = mysqli_query($dbConn,$sele); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Add Package</title> <link type="text/css" rel="stylesheet" href="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/css/bootstrap.css" /> <link type="text/css" rel="stylesheet" href="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/css/bootstrap.min.css" /> <link type="text/css" rel="stylesheet" href="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/css/bootstrap-theme.css" /> <link type="text/css" rel="stylesheet" href="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/css/bootstrap-theme.min.css" /> <script src="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/js/bootstrap.js"></script> <script src="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> <script src="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/js/npm.js"></script> </head> <style> body { background:#EEE; } </style> <body> <div class="container-fluid" style="background:green; height:70px;"> </div> <div class="container-fluid" style=" min-height:500px;"> <div class="row" style=" min-height:80px;"> <div class="col-lg-2" style="background:gray; height:1100px;"> <a class="btn btn-success" style="width:100%; margin-top:20px;" href="dashboard">Dashboard</a> <a class="btn btn-success" style="width:100%; margin-top:20px;" href="add user">Add Users</a> <a class="btn btn-success" style="width:100%; margin-top:20px;" href="add package">Add Packages</a> <a class="btn btn-success" style="width:100%; margin-top:20px;" href="all packages">View Packages</a> <a class="btn btn-success" style="width:100%; margin-top:20px;" href="send">Send Mail</a> <a class="btn btn-success" style="width:100%; margin-top:20px;" href="logout">Logout</a> </div> <div class="col-lg-10" style="background:#EEE; height:670px;"> <?php echo $error; ?> <div class="container" style=" margin-top:60px;background:white; box-shadow:5px 5px 5px gray; width:60%;"> <form class="" action="add package.php" method="post"> <div class="form-group"> <label>Track Number</label> <select class="form-control" name="track"> <?php while($row = mysqli_fetch_array($query)) { ?> <option><?php echo $row['track_code']; ?></option> <?php } ?> </select> </div> <div class="form-group"> <label>Date</label> <input type="text" class="form-control" name="date" style="height:40px;" placeholder="Date" /> </div> <div class="form-group"> <label>Time</label> <input type="text" class="form-control" name="time" style="height:40px;" placeholder="Time" /> </div> <div class="form-group"> <label>Activity</label> <input type="text" class="form-control" name="activity" style="height:40px;" placeholder="Activity" /> </div> <div class="form-group"> <label>Location</label> <input type="text" class="form-control" name="location" style="height:40px;" placeholder="Location" /> </div> <div class="form-group"> <label>Details</label> <input type="text" class="form-control" name="details" style="height:40px;" placeholder="Details" /> </div> <div class="container" style=" width:30%; margin-bottom:20px;"> <input class="btn btn-success" style="width:100%;" type="submit" name="submit" value="Save" /> </div> </form> </div> </div> </div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/ Share on other sites More sharing options...
gw1500se Posted January 10, 2020 Share Posted January 10, 2020 First please use the code icon (<>) and choose PHP. It makes your code much easier to read. Which is line 118? Did you echo all your variables to make sure they contain what you expect? 1 Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573259 Share on other sites More sharing options...
Frankice Posted January 10, 2020 Author Share Posted January 10, 2020 (edited) 35 minutes ago, gw1500se said: First please use the code icon (<>) and choose PHP. It makes your code much easier to read. Which is line 118? Did you echo all your variables to make sure they contain what you expect? <?php include('../connection.php'); session_start(); if (isset($_POST['submit'])){ $date = $_POST['date']; $location = $_POST['location']; $time = $_POST['time']; $track = $_POST['track']; $activity = $_POST['activity']; $details = $_POST['details']; { $query = "INSERT INTO package(date,location,time,track_code,activity,details)VALUES('$date','$location','$time','$track','$activity','$details')"; $check = mysqli_query($dbConn,$query); if($check) { header('location:success_package'); } else { $error = '<div class="alert-warning" style="height:60px;"> <p style="line-height:40px;">Error in posting</p> </div>'; } } } $sele = "SELECT * from details"; $query = mysqli_query($dbConn,$sele); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Add Package</title> <link type="text/css" rel="stylesheet" href="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/css/bootstrap.css" /> <link type="text/css" rel="stylesheet" href="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/css/bootstrap.min.css" /> <link type="text/css" rel="stylesheet" href="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/css/bootstrap-theme.css" /> <link type="text/css" rel="stylesheet" href="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/css/bootstrap-theme.min.css" /> <script src="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/js/bootstrap.js"></script> <script src="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> <script src="bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/js/npm.js"></script> </head> <style> body { background:#EEE; } </style> <body> <div class="container-fluid" style="background:green; height:70px;"> </div> <div class="container-fluid" style=" min-height:500px;"> <div class="row" style=" min-height:80px;"> <div class="col-lg-2" style="background:gray; height:1100px;"> <a class="btn btn-success" style="width:100%; margin-top:20px;" href="dashboard">Dashboard</a> <a class="btn btn-success" style="width:100%; margin-top:20px;" href="add user">Add Users</a> <a class="btn btn-success" style="width:100%; margin-top:20px;" href="add package">Add Packages</a> <a class="btn btn-success" style="width:100%; margin-top:20px;" href="all packages">View Packages</a> <a class="btn btn-success" style="width:100%; margin-top:20px;" href="send">Send Mail</a> <a class="btn btn-success" style="width:100%; margin-top:20px;" href="logout">Logout</a> </div> <div class="col-lg-10" style="background:#EEE; height:670px;"> <?php echo $error;\\this is the line I'm having issue with ?> <div class="container" style=" margin-top:60px;background:white; box-shadow:5px 5px 5px gray; width:60%;"> <form class="" action="add package.php" method="post"> <div class="form-group"> <label>Track Number</label> <select class="form-control" name="track"> <?php while($row = mysqli_fetch_array($query)) { ?> <option><?php echo $row['track_code']; ?></option> <?php } ?> </select> </div> <div class="form-group"> <label>Date</label> <input type="text" class="form-control" name="date" style="height:40px;" placeholder="Date" /> </div> <div class="form-group"> <label>Time</label> <input type="text" class="form-control" name="time" style="height:40px;" placeholder="Time" /> </div> <div class="form-group"> <label>Activity</label> <input type="text" class="form-control" name="activity" style="height:40px;" placeholder="Activity" /> </div> <div class="form-group"> <label>Location</label> <input type="text" class="form-control" name="location" style="height:40px;" placeholder="Location" /> </div> <div class="form-group"> <label>Details</label> <input type="text" class="form-control" name="details" style="height:40px;" placeholder="Details" /> </div> <div class="container" style=" width:30%; margin-bottom:20px;"> <input class="btn btn-success" style="width:100%;" type="submit" name="submit" value="Save" /> </div> </form> </div> </div> </div> </div> </body> </html> I'm facing database submission issues, please can someone with a good heart help me. Below is my code I don't know what I did wrong it won't submit mydata to the database it gives me error. Notice: Undefined variable: error in /home/psdelive/public_html/admin/add package.php on line 118 Edited January 10, 2020 by Barand Moved text out of code Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573260 Share on other sites More sharing options...
gw1500se Posted January 10, 2020 Share Posted January 10, 2020 What about my questions? Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573261 Share on other sites More sharing options...
Frankice Posted January 10, 2020 Author Share Posted January 10, 2020 Just now, gw1500se said: What about my questions? Just now, Frankice said: <?php echo $error; Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573263 Share on other sites More sharing options...
Frankice Posted January 10, 2020 Author Share Posted January 10, 2020 Just now, Frankice said: <?php echo $error;\\this is the line I'm having issue with ?> This Is the Line Sir. Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573264 Share on other sites More sharing options...
gw1500se Posted January 10, 2020 Share Posted January 10, 2020 (edited) ??? Now that you have used the code icon you can see there are syntax errors. Your <?PHP and ?> tags are messed up/missing. There are other glaring flaws in your code. Never ever put user data directly into a database. You are wide open to injection attacks. Use prepared statements and validate the data. I am guessing that you are not validating anything on the web page before submitting it and I see nothing that validates your data in your PHP script. Edited January 10, 2020 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573266 Share on other sites More sharing options...
Frankice Posted January 10, 2020 Author Share Posted January 10, 2020 Just now, gw1500se said: ??? Aside from the fact you did not answer my questions there are other glaring flaws in your code. Never ever put user data directly into a database. You are wide open to injection attacks. Use prepared statements as validate the data. I am guessing that you are not validating anything on the web page before submitting it and I see nothing that validates your data in your PHP script. Thank you sir, for now I'm concerning for the code to work properly like submitting the data to my database. My other file adding user works fine but this file having almost the same code is not submitting just giving me this error. Notice: Undefined variable: error in /home/psdelive/public_html/admin/add package.php on line 118 Sir, I can give you the link to help me look into it. Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573267 Share on other sites More sharing options...
gw1500se Posted January 10, 2020 Share Posted January 10, 2020 I edited my previous post after you replied. Please read. Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573268 Share on other sites More sharing options...
Frankice Posted January 10, 2020 Author Share Posted January 10, 2020 Sir, I just want my data to submit to the database. Please why can't the code submit the data the database? Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573269 Share on other sites More sharing options...
gw1500se Posted January 10, 2020 Share Posted January 10, 2020 Because it is not executing due to the syntax errors. Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573270 Share on other sites More sharing options...
Frankice Posted January 10, 2020 Author Share Posted January 10, 2020 Just now, gw1500se said: Because it is not executing due to the syntax errors. Please what can I do, I'm stumped? Is there any way you can help me fix it Sir? Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573271 Share on other sites More sharing options...
Barand Posted January 10, 2020 Share Posted January 10, 2020 In this code if($check) { header('location:success_package'); } else { $error = '<div class="alert-warning" style="height:60px;"> <p style="line-height:40px;">Error in posting</p> </div>'; } $error is set only if $check is false. Later on (where you get the error) you try to echo it when it hasn't been set. Also, you need to exit() from the script after that header() call to prevent the rest of the script from executing. Change the code to... $error = ''; if($check) { header('location:success_package'); exit; } else { $error = '<div class="alert-warning" style="height:60px;"> <p style="line-height:40px;">Error in posting</p> </div>'; } Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573272 Share on other sites More sharing options...
gw1500se Posted January 10, 2020 Share Posted January 10, 2020 I suggest you learn to read. I already told you what is wrong. Did you bother to look at the formatted code you posted and the colors? It is very obvious if you just look. That is the point of the the formatter. Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573273 Share on other sites More sharing options...
Barand Posted January 10, 2020 Share Posted January 10, 2020 The wrong colours in the code was caused by apostrophe in the text preceding the code. I have moved the text out and code looks pretty now. Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573274 Share on other sites More sharing options...
Frankice Posted January 10, 2020 Author Share Posted January 10, 2020 Just now, gw1500se said: I suggest you learn to read. I already told you what is wrong. Did you bother to look at the formatted code you posted and the colors? It is very obvious if you just look. That is the point of the the formatter. Okay Sir, I'm still a newbie. I'm trying it out now, so much thanks for your concern. Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573275 Share on other sites More sharing options...
ginerjm Posted January 10, 2020 Share Posted January 10, 2020 As a newbie you need to learn to focus your post on the parts that are giving you the problem. Posting a hundred lines of html code is not going to get the attention you need on the PHP code that is the source of your problem. Limit your posts to the parts you want us to help you with and since this is a PHP forum, all that extraneous html code is a lot of code to wade thru. Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573276 Share on other sites More sharing options...
Frankice Posted January 10, 2020 Author Share Posted January 10, 2020 Thank you sir, Here is the place I'm having issue. Quote if (isset($_POST['submit'])){ $date = $_POST['date']; $location = $_POST['location']; $time = $_POST['time']; $track = $_POST['track']; $activity = $_POST['activity']; $details = $_POST['details']; { $query = "INSERT INTO package(date,location,time,track_code,activity,details)VALUES('$date','$location','$time','$track','$activity','$details')"; $check = mysqli_query($dbConn,$query); if($check) { header('location:success_package'); exit; } else { $error = '<div class="alert-warning" style="height:60px;"> <p style="line-height:40px;">Error in posting</p> </div>'; } } } And at the line 122 when I want to echo the error gives error Notice: Undefined variable: error Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573277 Share on other sites More sharing options...
Frankice Posted January 10, 2020 Author Share Posted January 10, 2020 Just now, ginerjm said: As a newbie you need to learn to focus your post on the parts that are giving you the problem. Posting a hundred lines of html code is not going to get the attention you need on the PHP code that is the source of your problem. Limit your posts to the parts you want us to help you with and since this is a PHP forum, all that extraneous html code is a lot of code to wade thru. Thank you, I showed the full part incase is needed my problem is to insert the datas on the database. Is still giving me error Sir. Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573278 Share on other sites More sharing options...
gw1500se Posted January 10, 2020 Share Posted January 10, 2020 You need to figure out why $error is not being set. As a workaround initialize $error to something at the beginning of your code. In any case that has nothing to do with your database not being updated. Which problem are you trying to solve? Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573279 Share on other sites More sharing options...
Frankice Posted January 10, 2020 Author Share Posted January 10, 2020 Just now, gw1500se said: You need to figure out why $error is not being set. As a workaround initialize $error to something at the beginning of your code. In any case that has nothing to do with your database not being updated. Which problem are you trying to solve? When I submit the form it goes to the $error instead of success and it doesn't update in the database. Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573280 Share on other sites More sharing options...
gw1500se Posted January 10, 2020 Share Posted January 10, 2020 Obviously the query is failing. You need to echo mysqli_error to determine why. Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573281 Share on other sites More sharing options...
Frankice Posted January 10, 2020 Author Share Posted January 10, 2020 Please Can I send you link to the website I'm building Sir? Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573282 Share on other sites More sharing options...
Barand Posted January 10, 2020 Share Posted January 10, 2020 You still haven't ensured that error is always defined $error = ''; // <----------- YOU MISSED HIS LINE TO DEFINE $error if($check) { header('location:success_package'); exit; } else { $error = '<div class="alert-warning" style="height:60px;"> <p style="line-height:40px;">Error in posting</p> </div>'; } Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573283 Share on other sites More sharing options...
Frankice Posted January 10, 2020 Author Share Posted January 10, 2020 Please Just now, gw1500se said: Obviously the query is failing. You need to echo mysqli_error to determine why. Please can you add the mysqli_error to my code here if (isset($_POST['submit'])){ $date = $_POST['date']; $location = $_POST['location']; $time = $_POST['time']; $track = $_POST['track']; $activity = $_POST['activity']; $details = $_POST['details']; { $query = "INSERT INTO package(date,location,time,track_code,activity,details)VALUES('$date','$location','$time','$track','$activity','$details')"; $check = mysqli_query($dbConn,$query); if($check) { header('location:success_package'); exit; } else { $error = '<div class="alert-warning" style="height:60px;"> <p style="line-height:40px;">Error in posting</p> </div>'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/309817-im-facing-database-submission-issues/#findComment-1573284 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.