Date error displaying - php

Im doing a bit of php. First thing to clarify is that the * is the hidden text I didnt want to show. I was wandering why every time I try and type in a date e.g. 29/11/2013 it would come up on the php page as 0000-00-00. Could someone explain to me where I went wrong and give me a proper explanation on why and how it works, thanks very much.
<html>
<head>
<title>XYZ</title>
</head>
<body>
<h1>Insert into Database <i>XYZ</i> table <i>addressbook</i></h1>
<form method="get" action="********************************************* ">
<b>ID:</b> <input type="text" name="ID"><br>
<b>CARNAME:</b> <input type="text" name="CARNAME"><br>
<b>FUELTYPE:</b> <input type="text" name="FUELTYPE"><br>
<b>TRANSMISSION:</b> <input type="text" name="TRANSMISSION"><br>
<b>ENGINESIZE:</b> <input type="text" name="ENGINESIZE"><br>
<b>DOORS:</b> <input type="text" name="DOORS"><br>
<b>TOTAL:</b> <input type="text" name="TOTAL"><br>
<b>AVAILABLE:</b> <input type="text" name="AVAILABLE"><br>
<b>DATEADDED:</b> <input type="text" name="DATEADDED"><br>
<input type="submit">
</form>
</body>
</html>
<html>
<head>
<title>XYZ</title>
</head>
<body>
<h1>Insert into Database <i>XYZ</i> table <i></i></h1>
<?php
$link = mysql_connect ("**********", "*********", "*********");
mysql_select_db ("***********");
$ID = $_GET["ID"];
$CARNAME = $_GET["CARNAME"];
$FUELTYPE = $_GET["FUELTYPE"];
$TRANSMISSION = $_GET["TRANSMISSION"];
$ENGINESIZE = $_GET["ENGINESIZE"];
$DOORS = $_GET["DOORS"];
$TOTAL = $_GET["TOTAL"];
$AVAILABLE = $_GET["AVAILABLE"];
$DATEADDED = $_GET["DATEADDED"];
$query = "INSERT into CAR values ('$ID', '$CARNAME', '$FUELTYPE', '$TRANSMISSION', '$ENGINESIZE', '$DOORS', '$TOTAL', '$AVAILABLE', '$DATEADDED')";
$result = mysql_query ($query);
$query = "SELECT * from CAR";
$result = mysql_query ($query);
print "<table>";
print "<tr>";
print "<th>ID</th><th>CARNAME</th><th>FUELTYPE</th><th>TRANSMISSION</th><th>ENGINESIZE</th><th>DOORS</th><th>TOTAL</th><th>AVAILABLE</th><th>DATEADDED</th>";
print "</tr>";
for ($i = 0; $i < mysql_num_rows ($result); $i ++)
{
$row = mysql_fetch_object ($result);
print "<tr>";
print "<td>$row->ID</td>";
print "<td>$row->CARNAME</td>";
print "<td>$row->FUELTYPE</td>";
print "<td>$row->TRANSMISSION</td>";
print "<td>$row->ENGINESIZE</td>";
print "<td>$row->DOORS</td>";
print "<td>$row->TOTAL</td>";
print "<td>$row->AVAILABLE</td>";
print "<td>$row->DATEADDED</td>";
print "</tr>";
}
print "</table>";
mysql_close ($link);
?>
</body>
</html>

In MySql your data should of format yyyyMMdd so you should first convert it before inserting it
$DATEADDED = $_GET["DATEADDED"];
becomes something like
$DATEADDED = date("Y-m-d", strtotime($_GET["DATEADDED"]));

Check your date format in your database Y/m/d or if it set to date... Else change it to varchar (255) to be able to include any format.

Related

Data not fetched from MySQL table in PHP

I want to print the name and last name of an ID entered in the text box. Here is the PHP and HTML code:
<head>
<title>
Search your name by ID
</title>
</head>
<?php
if(isset($_POST["searchname"]))
{
$id = $_POST["searchname"];
$connect = new mysqli("localhost","adarsh","Yeah!","adarsh");
$a = mysql_query("Select * from users where id='$id'",$connect);
$row = mysql_fetch_assoc($a);
echo "$row[0] , $row[1] , $row[2]";
}
else
{
echo "error";
}
?>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" maxlength="6" name="searchname">
<input type="Submit" name="Submit">
</form>
</body>
Output when I enter ID:
, ,
There are entries in the MySQL table but I am unable to fetch them. What is wrong with my code?
UPDATE: I have also tried mysql_fetch_array but it is not working.
Main problem is that you're miximg mysqli and mysql. These are absolutely different APIs.
Assuming you have
$id = $_POST["searchname"];
$connect = new mysqli("localhost","adarsh","Yeah!","adarsh");
Next you should:
$result = $connect->query("Select * from users where id='$id'");
Then get results:
while ($row = $result->fetch_assoc()) {
var_dump($row);
}
And of course, instead of directly putting values into your query use prepared statements.
Update:
about mistakes:
Your main mistake is mixing apis. When you use mysql (which is deprecated and you mustn't use it anymore) you can't use any of mysqli functions and vice versa.
Next - as you create mysqli object with new, you should work in object-oriented style, i.e. calling methods from your mysqli object.
Try this:
<html>
<head>
<title>
Search your name by ID
</title>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" maxlength="6" name="searchname">
<input type="Submit" name="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST["searchname"])){
$id = $_POST["searchname"];
$connect = mysql_connect("localhost","adarsh","Yeah!","adarsh");
$result = mysql_query("Select * from users where id='$id'",$connect);
$row = mysql_fetch_assoc($result);
print_R($row);
}else{
echo "there is something wrong";
}

How do I use a user's input as 1 of the arguments for an sql function?

*This is a simplified version of the code
I have a query
SELECT func('123', x, '789');
where X is dependent on the user's input into a text box;
I tried
$result = pg_query($conn, "SELECT func('123', _GET['x'], '789')");
where x is retrieved from
<form method="get">
x: <input type="text" name="x"><br/>
<input type="submit">
</form>
however, this doesn't seem to work. Also, does the position of the PHP within the html make a difference?
Here is the entire code:
<!DOCTYPE html>
<html>
<body>
<?php
$string_connection = "host=localhost port=5432 user=myname dbname=mydb";
$conn = pg_connect($string_connection);
$result = pg_query($conn, "SELECT get_champ('123', $_GET['x'], '789')");
while ($row = pg_fetch_row($result)) {
echo "$row[0]";
echo "<br/>\n";
}
?>
<form method="get">
End Date: <input type="text" name="x"><br/>
<input type="submit">
</form>
</body>
</html>
Edit
Use:
$x = $_GET['x'];
then use ($conn, "SELECT get_champ('123', '$x', '789')")
$string_connection = "host=localhost port=5432 user=myname dbname=mydb";
$conn = pg_connect($string_connection);
$x = $_GET['x'];
$result = pg_query($conn, "SELECT get_champ('123', '$x', '789')");
while ($row = pg_fetch_row($result)) {
echo "$row[0]";
echo "<br/>\n";
}
and if it's an int, use $x = (int)$_GET['x'];
Original answer
name"x" missing = change to name="x"
also, either use $_GET['x'] or {$_GET['x']}
$result = pg_query($conn, "SELECT func('123',{$_GET['x']}, '789')");
and
<form method="get">
x: <input type="text" name="x"><br/>
<input type="submit">
</form>

Display count in the textbox

My goal to my code is to show the count query for the number of batchcode in table batchcodes to my textbox and if i click the button it will save to the batchcode table...my batchcode field is
'id','batchcode'
my current code:
<?php
ob_start();
?>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
include('include/connect.php');
$query = "SELECT DISTINCT count(batchcode) FROM batchcodes";
while( $rows = mysql_fetch_array($query)) {
}
?>
<?php
if(isset($_POST['save'])){
$var = $query+1;
$sql = "INSERT INTO batchcodes(batchcode) VALUES ('$var')";
}
?>
<form method="post" action="index.php" >
<input type="text" value="batch<?php echo $query; ?>" />
<input type="submit" name"save" />
</form>
</body>
</html>
In my code im suffering from error like Undefined variable query and warning mysql_fetch_array expects parameter 1...I need your help guys.
Use mysql_query.
$query = mysql_query("SELECT DISTINCT count(batchcode) AS nb_batchcode FROM batchcode");
while($row= mysql_fetch_array($query)) {
$batchcode=$row['nb_batchcode'];
}
<input type="text" name="save" value="batch<?php echo $batchcode; ?>" />
You should use mysql_query function to execute the query
$query = mysql_query("SELECT DISTINCT count(batchcode) as batchcode FROM batchcodes");
$sql = mysql_query("INSERT INTO batchcodes(batchcode) VALUES (". $var .")");
Here's how I did it on PHP based on my SQL data
<li>
<label> OR #: </label>
<?php
include('php/connect-db.php');
$sql = mysql_query("SELECT MAX(or_num)+1 AS inc_or FROM tbl_admission");
while($row = mysql_fetch_array($sql)){
$nextOR=$row['inc_or'];
}
?>
<input type="text" name="asID" value="<?php echo $nextOR; ?>" disabled>
OR_num is my integer and auto-incremented key in my table "tbl_admission" :)
Use $row instead of $query to get result from query.Try below code can help you.
<?php
ob_start();
?>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
include('include/connect.php');
$query = "SELECT DISTINCT count(batchcode) as batchcode FROM batchcodes";
while( $rows = mysql_fetch_array($query)) {
}
?>
<?php
if(isset($_POST['save'])){
$var = $row[0] + 1;
$sql = "INSERT INTO batchcodes(batchcode) VALUES (". $var .")";
}
?>
<form method="post" action="index.php" >
<input type="text" name="save" value="batch<?php echo $query; ?>" />
<input type="submit"
</form>
</body>
</html>

Checkboxes and text doesn't want to show the value

<!DOCTYPE html>
<html>
<head>
<title>Talenquiz2</title>
</head>
<body>
<?php
if (isset($_GET["controleer"]))
{
$vraag = $_GET["vraag"];
$juistantwoord = $_GET["juistantwoord"];
$foutantwoord1 = $_GET["foutantwoord1"];
$foutantwoord2 = $_GET["foutantwoord2"];
$con = mysql_connect("localhost","root","");
mysql_select_db("dbproject", $con);
$result = mysql_query("SELECT * FROM tblquizvragen");
while($row = mysql_fetch_array($result))
{
if ($row['vraag'] == $vraag)
{
if ($row['juistantwoord'] == $juistantwoord)
{
echo "Juist!<br />";
}
else
{
echo "Fout!<br />";
}
}
}
mysql_close($con);
echo "\n<hr />\n";
}
$aantalvragen=1;
$con = mysql_connect("localhost","root","");
mysql_select_db("dbproject", $con);
$result = mysql_query("SELECT * FROM tblquizvragen WHERE id='". $aantalvragen . "';");
$row = mysql_fetch_array($result);
The program is a quiz, it asks 5 questions with 3 chckbox 1 is corect and 2 are incorrect.
for ($aantalvragen=1; $aantalvragen<=5; $aantalvragen++)
{
$row = mysql_fetch_array($result);
}
her i lin
$vraag = $row['vraag'];
$juistantwoord = $row['juistantwoord'];
$foutantwoord1 = $row['foutantwoord1'];
$foutantwoord2 = $row['foutantwoord2'];
mysql_close($con);
?>
<form>
It doensn't show the values of the rows in my browser it shows only an open text and an open checkbox.
<input type="text" name="vraag" value="<?php echo $vraag; ?>" /><br />
<input type="checkbox" name="juistantwoord" value="<?php echo $juistantwoord; ?>" /><br />
<input type="checkbox" name="foutantwoord1" value="<?php echo $foutantwoord1; ?>" /><br />
<input type="checkbox" name="foutantwoord2" value="<?php echo $foutantwoord2; ?>" /><br />
<input type="submit" value="Controleer je antwoord" name="controleer" />
</form>
</body>
</html>
Your code is incorrect for fetching from the db
for(...) {
$row = mysql_fetch_array(...);
}
You simply loop over 5 lines of results, regardless of how many there may be, and assign the row array to $row... but do so for EVERY row without ever using them. So you end up trashing the first n-1 rows and come out of the loop with only row n saved.
If you're wrong with how many rows of data you're expecting, your 5-item loop may have only a 4-item result set to deal with, and the final row $row1 value will be the boolean FALSE that msyql_fetch returns when there's no more data.
Try something like this instead:
while($row = mysql_fetch_assoc($result)) {
echo ..... your stuff here ...
}
Far more reliable, doesn't depend on there being a known number of rows available, and will not output anything if there's no data at all.

PHP IF ELSE - If seems to be ignored?

i'm a bit of PHP noob, so sorry if this is a daft question, but I just can't figure this out by myself so any help would be greatly appreciated!
I am trying to create a modify page for an events web application. It seems to be working except when I try to validate the final if/else statement.
This statement returns the value of $row[0] and checks if its == NULL. If NULL, it should return an echo 'this event does not exist' to the user, if there is a value, it presents the user with a matrix of text boxes that they can change the data in.
Currently it works fine for the else statement when there is data found, but doesnt recognise the original if when there is no data. Coupled with that, the footer at the bottom of the page disappears!
Here is the main body of the code, i have highlighted the problem area. I understand that there is probably a more effective and efficient way of doing it all, but please keep it simple as I'm still learning. Thanks again. Dan
<div class="round">
<div id="main" class="round">
<span class="bold">MODIFY EVENT</span><br /><br />
On this page you can modify or delete the events that are stored on the database.<br />
Be aware that you cannot undo the DELETE function.<br />
<br />
Find Event:<br />
<table>
<form name="form1" id="form1" method="post" action="
<?php echo $_SERVER["PHP_SELF"]; ?>" >
<tr><th>By Date:</td><td><input type="text" name="modifyDate"
id="modifyDate" value="dd/mm/yy" /></td></tr>
<tr><th>By Name:</td><td><input type="text" name="modifyName" id="modifyName"
value="" /></td></tr>
<tr><th>Find All:</th><td><input type="checkbox" name="modifyAll"
id="modifyAll" /><td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Search" /></td></tr>
</form>
</table>
<?PHP
if(!isset($_POST['modify'])){
if(isset($_POST['submit'])){
$moddate = $_POST['modifyDate'];
If($moddate == "dd/mm/yy"){
$date = "";
}
else{
$newDate = str_replace("/",".",$moddate);
$wholeDate = $newDate;
$dateArray = explode('.', $wholeDate);
$date = mktime(0,0,0,$dateArray[1],$dateArray[0],$dateArray[2]);
}
$name = $_POST['modifyName'];
$all = $_POST['modifyAll'];
$host = "localhost";
$user = "user";
$password = "password";
$db = "database";
$con = mysql_connect($host,$user,$password) or die('Could not connect to Server');
$dbc = mysql_select_db($db, $con) or die('Could not connect to Database');
if($all != 'on'){
$q = "SELECT * FROM events WHERE date = '$date' || title = '$name' ";
}
else{
$q = "SELECT * FROM events";
}
$result = mysql_query($q);
$row = mysql_fetch_array($result) or die(mysql_error());
//THIS IS THE PROBLEM HERE!!!!
if($row[0]==NULL){
echo 'This event does not exist';
}
else{
?>
<form name="form1" id="form1" method="post" action="
<?phpecho $_SERVER['PHP_SELF']; ?>" >
<?PHP
$result = mysql_query($q) or die(mysql_error());
while ($row = mysql_fetch_array($result)){
$initialDate = date('d/m/y', $row['date']);
$ID = $row['ID'];
echo '<input type="text" name="inputEmail" id="inputEmail" value="'.$initialDate.'" />';
echo '<input type="checkbox" value=$ID name="toModify[]" style = "visibility: hidden;" /><br /><br />';
}
echo '<input type="submit" name="modify" value="Modify" />
<br /><br />';
}
}
}
else{
//modify database and return echo to user
}
?>
</div>
<div id="clear"></div>
</div>
</div>
</div>
<div id="footer" class="round shadow"></div>
</body>
</html>
mysql_fetch_array($result) returns false if there are no rows, so it's possible that even if there is nothing in the result, it's still returning a false and your if($row[0] == null) is evaluating to false, also.
In other words, you should be doing a more robust test on the return results from your query to catch fringe cases.
As others have mentioned or implied, here are some things you could / should be doing:
test for rows returned, not values in rows
test for existence of variable, not content of variable
check the database for errors returned
Is the field in the table it's pulling $row[0] from set to NOT NULL? If it is, it will never be a null value. Try instead something like (if empty($row[0]))
You could check the number of result rows to see if there are any events:
$result = mysql_query($q);
$numrows = mysql_num_rows ($result);
if($numrows === 0){
...

Categories