adding timestamp or datetime in php - php

I'm using php 5.5.3..In my scenario i have created a form to insert a task on which user have to fill 3 field namely description of task,due date and priority of task..immediately after that i have added a submit button..whenever i'm going to click on submit button the values should entered in database is my task. Now i wanna asked that how should i add due due date in database..i have tried with specific format but it doesn't work..my configuration id like :
Task.html :
<html>
<head>
<title>Creating a Personal To-Do List</title>
<h3>Add new Task</h3>
</head>
<body>
<form method="post" action="task.php">
Description :
<br/>
<input type="text" name="name"/>
<p>
Due date :
<br/>
<input type="text" name="date" size="20"/>
<p><br/>
Priority :
<br/>
<select name="priority">
<option name="high">High</option>
<option name="medium">Medium</option>
<option name="low">low</option>
</select>
<p>
<input type="submit" name="submit" value="Save Task">
</form>
</body>
</html>
And Task.php :
<?php
$db_hostname="localhost";
$db_database="music";
$db_username="root";
$db_password="p3";
if(isset($_POST['submit']))
{
if(empty($_POST['name']))
die("Error : Task name is required");
$name=$_POST['name'];
if(empty($_POST['date']))
die("Error : Please enter a valid date");
$task_date = $_POST['date'];
if(empty($_POST['priority']))
die("Error : Please select a priority");
$priority=$_POST['priority'];
$db_server=mysql_connect($db_hostname,$db_username,$db_password);
if(!$db_server)
{
die("Error:unable to connect to server");
}
else
echo "Connected to server";
mysql_select_db($db_database)
or die("Error:unable to connect to database");
echo "</br>Connected to $db_database database";
$task_date = date('Y-m-d');
echo $task_date;
$insert_query=mysql_query("Insert into task(name,due,priority)values('$name','$task_date','$priority'))");
if(($insert_query)==TRUE)
{
echo "<br/>";
echo "New Task inserted ";
}
}
?>
Please let me know where should i make changes?or is their any another way to do this?..

Try this:
$insert_query=mysql_query("Insert into task(name,due,priority)values('$name','$task_date','$priority')")
Instead of this:
$insert_query=mysql_query("Insert into
task(name,due,priority)values('$name','$task_date','$priority'))")
Consider using PDO or Mysqli, Mysql is deprecated.

Try this it works.
$insert_query=mysql_query("INSERT INTO task(name,due,priority) VALUES ('".$name."','".$task_date."','".$priority."')") or die(mysql_error());
mysql_error() function will tell the errors if data not inserted... and you are using this '$priority'))"); double braces...

Related

I want to delete a selected item that is in combobox using PhP and that must also reflect in Database

This is the php code to delete.
<?php
$namez = $_POST['vendel'];
$name = mysql_real_escape_string($namez);
mysql_connect("localhost","root",
"") or die("Wrong username or password");
mysql_select_db("vendor") or die( "Unable to select database");
mysql_query("DELETE FROM vendoradd WHERE venname='$name'");
?>
And here is the code where I select from a combobox.
<form action="delete.php" method="post">
<select name="vendel">
<?php
$con=mysqli_connect("localhost","root","","vendor") or die("Unable to connect");
$query = mysqli_query($con,"select venname from vendoradd");
while ($row=$query->fetch_assoc()){
echo "<option value=\"vendel1\">".$row['venname']."</option>";
}
?>
</select>
<input type="submit" value="Delete" id="del">
</form>
I am not getting any error, the page is redirecting to delete.php file but when I cross check the database the selected data is not deleted. Any help is appreciated. Thanks in advance
Well... Its because you're posting a value "vendel1", which means that you're always sending the same value, just for you to know... The name of the select combobox is the index in the $_POST and the value in the option is the value, for example, if you have:
<form action="" method="post">
<select name="foo">
<option value="1">Stack</option>
<option value="2">Overflow</option>
</select>
<input type="submit">
</form>
<?php
echo $_POST['foo'];
?>
And you select the option "Stack" the output of
$_POST['foo']
will be 1.
So if you re going to post with a select combobox, you'll have to give an unique value to each option, so you can delete the correct one in your database. My recommendation for you is to create an unique ID to each user in your table, its better to compare unique ID's than strings... Why? Because if you have two people with the same name, you'll delete both, but, with an unique ID you'll not have that problem. If you want to delete
<form action="delete.php" method="post">
<select name="vendel">
<?php
$con=mysqli_connect("localhost","root","","vendor") or die("Unable to connect");
$query = mysqli_query($con,"select TRIM(venname) from vendoradd");
while ($row=$query->fetch_assoc()){
echo "<option value=".$row['venname'].">".$row['venname']."</option>";
}
?>
</select>
<input type="submit" value="Delete" id="del"></form>
And your delete should be like this:
<?php
$namez = $_POST['vendel'];
$name = mysql_real_escape_string($namez);
mysql_connect("localhost","root", "") or die("Wrong username or password");
mysql_select_db("vendor") or die( "Unable to select database");
mysql_query("DELETE FROM vendoradd WHERE TRIM(venname) = TRIM('$name')");
?>

Data not updating in SQL databse by PHP

Well I am new to coding,What I am trying is to update data in database through Php .I am trying hard to update data but i don't know where is problem coming ,there is no error too.My first file is
"ppp.html"
<html>
<form action="l.php"method="post">
<input type ="text" name ="complaint">
</input>
<input type="submit"></input>
</html>
Now my "L.php "
It also don't show any error .It goes through easily
<?php
$complaint="";
if(
isset($_POST['complaint']))
{$complaint =$_POST['complaint'];}
mysql_connect("localhost","root","") or die ("couldnt attack ");
mysql_select_db("site")or die('i surrender');
$query=("SELECT * FROM site2 where category='$complaint'") or die("couldnt select");
$result=mysql_query($query) or die ('hghyt');
while ($complaint= mysql_fetch_array($result))
{
echo"<td>".'<br>'.$complaint['category']."</tr>";
ECHO"<TR>"."<A HREF='update.php'>"."UPDATE"."</A>";
echo "<br/>";
ECHO"</table>";
}
?>
Sorry for very wrong query and very inappropriate way of coding but I am learning it all by myself through internet
Now my "update.php file"
<html>
<form action="update1.php" method="post">
<input type= "text" name="blue"></input>
<input type= "submit"></input>
</form>
</html>
It also goes of in easy way,and don't show any problem ,now my last file "update1.php"
<?php
$complaint="";
if(isset ($_POST['complaint']))
{$complaint =$_POST['complaint'];}
$blue="";
if(isset ($_POST['blue']))
{$blue =$_POST['blue'];}
mysql_connect("localhost","root","") or die ("couldnyt coibnovdbs");
mysql_select_db("site") or die ("no databse");
$query=("update site2 set category='$blue' where category ='$complaint'") or die ("couldnt attack");
$result=mysql_query($query) or die("kjkk");
?>
Please help me .It is bothering me,I cant find any solution for it.I think problem is in last file only but it is not showing any errors.
Thank you
Considering the flow of your website, update1.php never receives the value in $complaint, hence fails to update. You need to pass the value to it.
For example, the following edits should suffice.
Edit L.php
echo"<td>".'<br>'.$complaint['category']."</tr>";
ECHO"<TR>"."<A HREF='update.php?complaint=".$complaint['category']."'>"."UPDATE"."</A>";
Edit update.php
<html>
<form action="update1.php" method="post">
<input type="hidden" name="complaint" value="<?php echo $_GET['complaint'] ?>"
<input type= "text" name="blue"></input>
<input type= "submit"></input>
</form>
</html>

Using php how can I update a row in mysql database based on a forms input value?

I have a form that has various inputs. One of the inputs contains a pre determined value that is loaded by script that is named "invoiceid". This invoiceid is the name and id of the form input and also the name of a column in my database. How can update the row in my database that contains the same value under the invoiceid column with all other form data submitted? I dont know what I'm doing...please help. Thanks for your time.
FORM
<form action="update.php" id="contactForm" method="post">
<input id="invoiceid" name="invoiceid" type="hidden" value=""/>
<input id="txt1" name="txt1" type="text" value=""/>
<input id="q1" name="q1" value="9.50" checked="checked" type="radio">
<input id="q1" name="q1" value="12.50" type="radio">
<select id="selectbox" name="selectbox">
<option selected="selected" value="">Please select...</option>
<option value="PURCHASE">Order for Purchase</option>
<option value="REVIEW">Order for Review</option>
</select>
<button id="btn1" type="submit" name="submit">Submit</button></div>
</form>
update.php
//Table name: seguin_orders
<?php
// Create the connection to the database
$con=mysqli_connect("xxx","xxx","xxx","xxx");
// Check if the connection failed
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
if (isset($_GET['invoiceid']))
{
$invoiceid= $_GET['invoiceid'];
}
?>
I think it can help you.
update.php
<?php
// Create the connection to the database
$con=mysqli_connect("xxx","xxx","xxx","xxx");
// Check if the connection failed
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
if (isset($_POST['invoiceid']))
{
$invoiceid= $_POST['invoiceid'];
$column1 = $_POST['txt1'];
$column2 = $_POST['q1'];
$column3 = $_POST['selectbox'];
$sql = "UPDATE TableName SET column1='".$column1."', column2='".$column2."', column3='".$column3."' WHERE invoiceid='".$invoiceid."'";
}
?>
///note: change the table name and column names according to your database

retrieving data from database to dropdown list

I would like to display a drop down list with data retrieved from a table with only one column
this is my form
<html>
<head>
</head>
<body>
<form action="insert_customer_complaint.php" method="post">
Name: <input type="text" name="name"><br>
Complaint: <input type="text" name="comp"><br>
Reason: <select name="reason">
Add <input type="submit">
</form>
</body>
</html>
this the php file i use to insert data
<?php
// Create connection
$con=mysqli_connect("localhost","ccc","ccc","ccc");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO reason (reason_name)
VALUES
('$_POST[reason_name]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
Inserting part works very well I need to get data for the drop down list
from a table with one column using the same php file
table name = reason, column name = reason_name
please help me out
I have manged to insert data and every thing works well. now i want to generate report in a table from where the the column name should be retrieved from from the data in a particular row here's what i need
please see click for image
http://testserverforprojects.tk/CC/tables.JPG
table should be dynamic because it should have a latest year at the end for instance this year only will have data upto 2013 so 2013 will be the last column.. so in 2015, 2014 will be the last column
Use this:
<html>
<head>
</head>
<body>
<form action="insert_customer_complaint.php" method="post">
Name: <input type="text" name="name"><br>
Complaint: <input type="text" name="comp"><br>
Reason: <select name="reason">
<?php
$con=mysqli_connect("localhost","ccc","ccc","ccc");
if (mysqli_connect_errno())
{echo "Failed to connect to MySQL: " . mysqli_connect_error();}
$sql="SELECT * FROM reason";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result))
{
echo '<option value='.$row['reason_name'].'>'.$row['reason_name'].'</option>';
}
?>
</select>
Add <input type="submit">
</form>
</body>
</html>
Wouldn't it just be :
$sql="SELECT reason_name From reason";
? Then iterate over the returned set and build the related html to accomplish your interface requirements

data from droplist to database mysql

I need advice, what i did wrong and this code not working. In short I have droplist menu with data read from mysql database and I want this data what user selected put in to another table/row in db. with present code I received only NULL value in inserted row... some I assume maybe something wrong with syntax, I tried search similar topic and tried different way but result is same :| This is my code :
get function and form
<br><br>
<?php
include 'connectdb.php';
$sql="select * from persons";
$result=mysqli_query($con,$sql);
while ($row=mysqli_fetch_array($result)) {
$id=$row["id"];
$name=$row["name"];
$name_done.="<OPTION VALUE=\"$id\">".$name;
}
?>
<form action="insert.php" method="post">
<SELECT name="name_done" id="nane_done">
<OPTION VALUE=0>Choose Your name :
<?=$name_done?>
</SELECT> <br>
RFC: <input type="text" name="number"><br>
Date: <input type="text" id="datepicker" name="date">
<input type="submit" value="submit" />
</form>
And Insert
<?php
include 'connectdb.php';
$name_done = $_POST['nane_done'];
mysqli_query($con,"INSERT INTO rfc(name_done) VALUES (.$name_done)");
---- below working OK----
$sql = "INSERT INTO rfc(number,date)
VALUES
('$_POST[number]','$_POST[date]')";
if (!mysqli_query($con,$sql,$name_done))
{
die('Error: ' . mysqli_error($con));
}
echo "RFC added";
mysqli_close($con);
?>
You have a typo - "nane_done" rather than "name_done" in this line: $name_done = $_POST['nane_done'];.

Categories