form not adding to database after submitting it - php

I havet a user event form that ive created and it wont add anything to database ive checked all form elements against the database columns and read several other comments on here about this but nothing is happening can someone possibly please check this over for me many thanks in advance jan x
<?php
include ('config/db_connect.php');
include("config/ckh_session.php");
if (isset($_POST['submit'])) {
$event_type = mysqli_real_escape_string($conn, $_POST['event_type']);
$event_date = mysqli_real_escape_string($conn, $_POST['event_date']);
$event_country = mysqli_real_escape_string($conn, $_POST['event_country']);
$event_postcode = mysqli_real_escape_string($conn, $_POST['event_postcode']);
$event_title = mysqli_real_escape_string($conn, $_POST['event_title']);
$event_description = mysqli_real_escape_string($conn, $_POST['event_description']);
$event_ltm = mysqli_real_escape_string($conn, $_POST['event_ltm']);
$query = "INSERT INTO meets (`event_type`,`event_date`,`event_country`,`event_postcode`,`event_title`,`event_description`,`event_ltm`) VALUES ('$event_type','$event_date','$event_country','$event_postcode','$event_title','$event_description','$event_ltm')";
$result = mysqli_query($conn,$query);
} else {
mysqli_close($conn);
}
?>
And here is the database table
CREATE TABLE IF NOT EXISTS `meets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`event_type` varchar(50) NOT NULL,
`event_date` varchar(50) NOT NULL,
`event_country` varchar(50) NOT NULL,
`event_postcode` varchar(50) NOT NULL,
`event_title` varchar(255) NOT NULL,
`event_description` text NOT NULL,
`event_ltm` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Ok, make sure that in your form inputs, the name attribute matches with the post values that you are getting. Next, make sure that the form action is pointing to the correct php file, and that the form method is POST. Also, ensure that the form is posting the values. Your php to insert into the database is wrapped in an if->isset for the value 'submitted', so I would also ensure that that value is being passed.

Related

how to insert correct date and ID in mysql table

I have a simple form with following fields
Auditor, departement, date, weeknr, registration (i have them in dutch..)
PHP is a problem since i don't know it too well.
So 2 question's here:
My date field is a jQuery datepicker and returns (07/19/2019)
but in mysql I only get 0000-00-00,
Also i would like to have an ID based on the values of some of those fields that a user fills in. Already tried to search how to do it but can't find good examples.
So my ID would be something like: departement_weeknr (for example..)
I looked at other examples of code online, but since i don't know PHP it is difficult for me to integrate it.
This is my code how I store all the information
if (isset($_POST['register_form'])) {
// receive all input values from the form
$Auditor = mysqli_real_escape_string($db, $_POST['Auditor']);
$Afdeling = mysqli_real_escape_string($db, $_POST['Afdeling']);
$Datum = mysqli_real_escape_string($db, $_POST['Datum']);
$Week = mysqli_real_escape_string($db, $_POST['Week']);
$Aanmelding = mysqli_real_escape_string($db, $_POST['Aanmelding']);
$query="INSERT INTO 6saudits (Auditor,Afdeling,Datum,Week,Aanmelding) VALUES ('$Auditor','$Afdeling','$Datum','$Week','$Aanmelding')";
$datum = date('Y-m-d', strtotime(str_replace('-', '/', $datum)));
mysqli_query($db, $query);
I hope someone can help me with the date and ID part, so it would be stored properly in mysql table
Edit SQL table:
CREATE TABLE IF NOT EXISTS `6saudits` (
`ID` int(50) NOT NULL AUTO_INCREMENT,
`Auditor` varchar(50) NOT NULL,
`Afdeling` varchar(50) NOT NULL,
`Datum` date NOT NULL,
`Week` int(20) NOT NULL,
`Aanmelding` varchar(50) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=17 ;
First Assign the Date value in $Datum. And check the variable name also.'$datum' and '$Datum' are not equal. Then write insert query. so your query like as follwing..
if (isset($_POST['register_form'])) {
// receive all input values from the form
$Auditor = mysqli_real_escape_string($db, $_POST['Auditor']);
$Afdeling = mysqli_real_escape_string($db, $_POST['Afdeling']);
$Datum = mysqli_real_escape_string($db, $_POST['Datum']);
$Week = mysqli_real_escape_string($db, $_POST['Week']);
$Aanmelding = mysqli_real_escape_string($db, $_POST['Aanmelding']);
$Datum = date('Y-m-d', strtotime(str_replace('-', '/', $Datum)));
$query="INSERT INTO 6saudits (Auditor,Afdeling,Datum,Week,Aanmelding) VALUES ('$Auditor','$Afdeling','$Datum','$Week','$Aanmelding')";
mysqli_query($db, $query);

Why won't my array get inserted into my Database?

I have this code and for some reason it won't get inserted into my database. It's basically taking an array, turning it into a string and then submit the values.
(If you need me to edit to show my whole code, I will do so)
Code I am having issues with down below
$array = array($RaceNumber,$Track,$Num,$HorseName,$Odds,$Color,$Jockey,$Trainer,$PostTime,$Course,$RaceDistance,$Win,$Place,$Show);
for ($a=0; $a<$Num; $a++) {
$dataArray=array($RaceNumber[$a],$Track[$a],$Num[$a],$HorseName[$a],$Odds[$a],$Color[$a],$Jockey[$a],$Trainer[$a],$PostTime[$a],$Course[$a],$RaceDistance[$a],$Win[$a],$Place[$a],$Show[$a]);
$dataArray--;
for ($j=0; $j<$Num; $j++) {
$RaceNumber=$dataArray[0];
$Track=$dataArray[1];
$Num=$dataArray[2];
$HorseName=$dataArray[3];
$Odds=$dataArray[4];
$Color=$dataArray[5];
$Jockey=$dataArray[6];
$Trainer=$dataArray[7];
$PostTime=$dataArray[8];
$Course=$dataArray[9];
$RaceDistance=$dataArray[10];
$Win=$dataArray[11];
$Place=$dataArray[12];
$Show=$dataArray[13];
$sql="INSERT INTO `$Date` (RaceNumber,Track,HorseNum,HorseName,Odds,Color,JockeyName,TrainerName,PostTime,Course,RaceDistance,Win,Place,Show) VALUES ('$RaceNumber','$Track','$Num','$HorseName','$Odds','$Color','$Jockey','$Trainer','$PostTime','$Course','$RaceDistance','$Win','$Place','$Show')";
echo $sql;
mysqli_query($query2,$sql);
}
}
when I echo my $sql I get
INSERT INTO 2018-09-20 (RaceNumber,Track,HorseNum,HorseName,Odds,Color,JockeyName,TrainerName,PostTime,Course,RaceDistance,Win,Place,Show) VALUES ('1','FingerLakes','1','','','Red','','','','Dirt','','none','none','none')
But when I do my query, it isn't inserting into database.
Part of my code where I create the datatable
<?php
if(isset($_POST['submit'])) {
$Date = $_POST['date'];
$sql = "CREATE TABLE IF NOT EXISTS `$Date` (
`Id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`RaceNumber` varchar(255) NOT NULL,
`Track` varchar(255) NOT NULL,
`HorseNum` varchar(255) NOT NULL,
`HorseName` varchar(255) NOT NULL,
`Odds` varchar(255) NOT NULL,
`Color` varchar(255) NOT NULL,
`JockeyName` varchar(255) NOT NULL,
`TrainerName` varchar(255) NOT NULL,
`PostTime` varchar(255) NOT NULL,
`Course` varchar(255) NOT NULL,
`RaceDistance` varchar(255) NOT NULL,
`Win` varchar(255) NOT NULL,
`Place` varchar(255) NOT NULL,
`Show` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8"
;
$query2 = mysqli_connect('localhost','root','','Races');
$z= mysqli_query($query2, $sql) or die("Table already exist.. please try again");
echo "Your Table ".$Date." is successfully created <br/>";
$RaceNum = $_POST['RaceNum'];
$i=1;
I am receiving in my error log of
2018-09-20 16:00:59 9444 [ERROR] Incorrect definition of table mysql.column_stats: expected column 'max_value' at position 4 to have type varbinary(255), found type varchar(255).
You are using a column named Show that's a reserved keyword in mysql, add backquotes to it and the insert query should work.
It's worth noting that you shouldn't name your table with only digits and hyphens.
For reference here is the complete list of the reserved keywords:
https://dev.mysql.com/doc/refman/8.0/en/keywords.html

Issue trying to insert data into a MySQL table with php

I have an existing table and I'm trying to insert data to it from a form submission.
I think I need another set of eyes to look over my code. There is an issue with it, at this point I'm not sure what.
I fill out the form, it appears to submit, I check the table and I get nothing. No data gets inserted.
If some of the values aren't being used right now, can that hinder the data from being inserted into the table? Shouldn't the form inputs that are on the form be inserted anyway? I tried to use only the values within the form, and that still did not work.
For example, right now the form inputs are only for first, last, title, genre, about, and picture. The rest only exist within the table. Data for the remaining fields cannot be currently entered at this time.
Hopefully someone has a solution?
$servername = "server";
$username = "user";
$password = "pass";
$dbname = "db";
if (isset($_POST['submit'])){
$conn = mysqli_connect($servername,$username,$password,$dbname);
if (!$conn) {
die("Connection failed: " . mysqli_error());
}
$first = mysqli_real_escape_string($conn, $_POST['First']);
$last = mysqli_real_escape_string($conn, $_POST['Last']);
$title = mysqli_real_escape_string($conn, $_POST['Title']);
$storylink = mysqli_real_escape_string($conn, $_POST['StoryLink']);
$genre = mysqli_real_escape_string($conn, $_POST['Genre']);
$about = mysqli_real_escape_string($conn, $_POST['About']);
$link = mysqli_real_escape_string($conn, $_POST['Link']);
$picture = mysqli_real_escape_string($conn, $_POST['Picture']);
$alt = mysqli_real_escape_string($conn, $_POST['ALT']);
$sql = "INSERT INTO ContrTemp (`First`,`Last`,`Title`,`StoryLink`,`Genre`,`About`,`Link`,`Picture`,`ALT`)
VALUES ('$first','$last','$title','$storylink','$genre','$about','$link','$picture','$alt')";
mysqli_query($conn, $sql) or die('Error: ' . mysqli_error($conn));
mysqli_close($conn);
}
Here is one input field from the form. The others are pretty much the same.
<input type="text" id="ContrTitle" name="Title" placeholder="Title" class="inputFields" style="width:650px;" />
Could there be an issue with the name within the input?
sql table structure:
CREATE TABLE IF NOT EXISTS `ContrTemp` (
`ID` int(5) NOT NULL AUTO_INCREMENT,
`First` varchar(40) COLLATE utf8_unicode_ci NOT NULL,
`Last` varchar(40) COLLATE utf8_unicode_ci NOT NULL,
`Title` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`StoryLink` varchar(140) COLLATE utf8_unicode_ci NOT NULL,
`Genre` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
`About` varchar(2000) COLLATE utf8_unicode_ci NOT NULL,
`Link` varchar(125) COLLATE utf8_unicode_ci NOT NULL,
`Picture` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`ALT` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Now I'm actually hitting the database, a record shows up, but no data was inserted. Before, No record was inserted at all.
You can try this one for inserting data
$sql = 'INSERT INTO ContrTemp (First,Last,Title,StoryLink,Genre,About,Link,Picture,ALT) VALUES ("'.$first.'","'.$last.'","'.$title.'","'.$storylink.'","'.$genre.'","'.$about.'","'.$link.'","'.$picture.'","'.$alt.'")';
I think you have done pretty well but one last try put these symbols [backticks] ` around your fields to understand mysql they are just fields.
$sql = "INSERT INTO ContrTemp (`First`,`Last`,`Title`,`StoryLink`,`Genre`,`About`,`Link`,`Picture`,`ALT`)
VALUES ('$first','$last','$title','$storylink','$genre','$about','$link','$picture','$alt')";
Your mysqli_error() is incorrect syntax, you need to specify the connection so have mysqli_error($conn) to properly feedback to you the SQL errors. As mentioned by Mitkosoft in comments some of your column names are MySQL keywords. It could be a good habit to get into to always encase column names in backticks.
So:
$sql = "INSERT INTO ContrTemp (`First`,`Last`,`Title`,`StoryLink`,`Genre`,`About`,`Link`,`Picture`,`ALT`)
VALUES ('$first','$last','$title','$storylink','$genre','$about','$link','$picture','$alt')";
mysqli_query($conn, $sql) or die('Error: ' . mysqli_error($conn));
Also you don't need to select db with mysqli_select_db($conn,$dbname); because the database is selected on the login command mysqli_connect above.
Further queries:
Is your database access account allowed to INSERT to this database?
Can you insert the same data as in your form directly into the database using an interface such as PHPMyAdmin, MySQL Workbench (Or whichever one you use to view the DB)?
I had hoped this had been as simple as the code I provided above, thus the reason I did not mention that this code was being used as part of a WordPress template. I did not think that would be come an issue as the template is pretty light weight.
With that said, I simply took my block of php code that handles the data insertion, and I placed it at the top of the template. Worked like a charm..

Mysqli not finding row

i am trying to let a user login here is my login script atm im just checking if the username exists but for some reason its not finding any records even though the user name is right
<?php if(isset($_POST)){
print_r($_POST);
//Variables from the table
$usernamelogin = $_POST['usernamelogin'];
$passwordlogin = $_POST['passwordlogin'];
//Prevent MySQL Injections
$usernamelogin = stripslashes($usernamelogin);
$passwordlogin = stripslashes($passwordlogin);
$usernamelogin = mysqli_real_escape_string($con, $usernamelogin);
$passwordlogin = mysqli_real_escape_string($con, $passwordlogin);
$loginquery = mysqli_query($con,"SELECT * FROM reg_users WHERE user ='$usernamelogin' AND authorised ='1'") or die("Can not query DB.");
$logincount = mysqli_num_rows($loginquery);
if($logincount == 1){
echo "user exists";
} else {
echo "User doesnt exist";
}
}
?>
my table is called reg_users and user is the column the username goes into. i am doing the same thing on register and that works
Any ideas guys?
table is
`reg_users` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`firstname` varchar(30) DEFAULT NULL,
`surname` varchar(30) DEFAULT NULL,
`user` varchar(255) DEFAULT NULL,
`password` varchar(512) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`banned` int(1) DEFAULT '0',
`authorised` int(1) DEFAULT '0',
`activationcode` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1;
And the sqlfiddle is here
Here is the config.php which is called in the header of the page
<?php //Information to connect to your MySQL Server AND DB
$hostdb = "localhost";
$userdb = "test_nathan";
$passworddb = "xxxxx";
$db = "test_nathan";
//Connect to MySQL Server
$con = mysqli_connect($hostdb,$userdb,$passworddb,$db) or die ("could not connect");
session_save_path('../login/sessions');
require_once('functions.php');
?>
Figured out the problem guys for some reason when i was storing the variables on escaping the string there was spaces attached to the variable. thanks for the help guys.

Previous updated record reverts when updating a new record MySQL PHP

Good day to all. I'm trying to make a task tracking module for my system. The logic is, whenever I assign a task to a user, the system updates the task to "IsTaken" meaning the particular user is responsible for that task. Upon updating, it creates an entry into "user_task" table which basically ties together the task and the user table. Whenever I assign a task to somebody it's fine. But when I do it again, the previous task record's IsTaken field reverts back to 0. I try to re-assign it, but again, the previous record reverts. It's quite weird. I'm using XAMPP, MySQL and PHP. I'm hoping I'm not the only one experiencing this. Any help would be much appreciated.
Here are my tables:
CREATE TABLE IF NOT EXISTS `task` (
`Task_No` int(11) NOT NULL AUTO_INCREMENT,
`Task_Name` varchar(100) NOT NULL,
`Task_Desc` varchar(450) DEFAULT NULL,
`Task_DateCreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Task_IsTaken` tinyint(1) NOT NULL,
PRIMARY KEY (`Task_No`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `user_task` (
`UT_No` int(11) NOT NULL AUTO_INCREMENT,
`User_Email` varchar(100) NOT NULL,
`Task_No` int(11) NOT NULL,
`Task_Duration` varchar(20) NOT NULL,
`Task_DateTaken` date DEFAULT NULL,
`Task_DateFinished` timestamp NULL DEFAULT NULL,
`Task_IsIssue` tinyint(1) NOT NULL,
PRIMARY KEY (`UT_No`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
And here are my scripts (from the form):
//Process - Insert Task
if(isset($_POST['btnCreateTask']))
{
if($_POST['taskName']!=NULL)
{
$taskName = mysql_real_escape_string ($_POST['taskName']);
$taskDesc = mysql_real_escape_string ($_POST['taskDesc']);
$insertTask = "INSERT INTO task(Task_Name, Task_Desc, Task_IsTaken) VALUES('$taskName', '$taskDesc', 0)";
$sqlResult1 = mysql_query($insertTask);}
else
{
echo "No task name given";
$errorCode = 1;
}
}
if(isset($_POST['btnAssignTask']))
{
if($_POST['assignTaskName']!=NULL)
{
$assigntaskName = mysql_real_escape_string($_POST['assignTaskName']);
$assigntaskNo = mysql_real_escape_string($_POST['assignTaskNo']);
$assigntaskOwner = mysql_real_escape_string($_POST['assignTaskOwner']);
$assigntaskDuration = mysql_real_escape_string($_POST['assignTaskDuration']);
$updateUpcomingTask = "UPDATE task SET Task_IsTaken = '1' AND Task_No = '$assigntaskNo'";
$createUserTask = "INSERT INTO user_task (User_Email, Task_No, Task_Duration, Task_DateTaken, Task_DateFinished)
VALUES ('$assigntaskOwner', '$assigntaskNo', '$assigntaskDuration', '$now', NULL)";
$sqlResult2 = mysql_query($updateUpcomingTask);
$sqlResult3 = mysql_query($createUserTask);
}
else
{
echo "No task selected";
$errorCode = 2;
}
}
Your code should be changed to:
if(isset($_POST['btnAssignTask']))
{
if($_POST['assignTaskName']!=NULL)
{
$assigntaskName = mysql_real_escape_string($_POST['assignTaskName']);
$assigntaskNo = intval($_POST['assignTaskNo']);
$assigntaskOwner = mysql_real_escape_string($_POST['assignTaskOwner']);
$assigntaskDuration = mysql_real_escape_string($_POST['assignTaskDuration']);
$updateUpcomingTask = "UPDATE task SET Task_IsTaken = '1' WHERE Task_No = $assigntaskNo";
$createUserTask = "INSERT INTO user_task (User_Email, Task_No, Task_Duration, Task_DateTaken, Task_DateFinished)
VALUES ('$assigntaskOwner', '$assigntaskNo', '$assigntaskDuration', '$now', NULL)";
$sqlResult2 = mysql_query($updateUpcomingTask);
$sqlResult3 = mysql_query($createUserTask);
}
else
{
echo "No task selected";
$errorCode = 2;
}
}

Categories