Data not inserting - php

I have created a simple web form in which you input data into and then it is posted into the database, when submitting the information it is submitted correctly but when I view it in the table the data is invisible.
query
CREATE TABLE `recipe` (
`id` int(4) NOT NULL auto_increment,
`recipename` varchar(65) NOT NULL default '',
`ingredients` varchar(65) NOT NULL default '',
`instructions` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=0 ;
php code
<?php
$host="localhost"; // Host name
$username="my username"; // Left empty due to privacy
$password="mypassword"; // Left empty due to privacy
$db_name="mydatabase"; // Left empty due to privacy
$tbl_name="recipe"; // Table name
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$recipe=$_POST['recipename'];
$ingredients=$_POST['ingredients'];
$instructions=$_POST['instructions'];
$sql="INSERT INTO $tbl_name(recipename, ingredients, instructions)VALUES('$recipe', '$ingredients', '$instructions')";
$result=mysql_query($sql);
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='recipe.php'>Back to main page</a>";
} else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>
Web Form
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td>
<form name="form1" method="post" action="insert_ac.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Insert Data Into mySQL Database </strong></td>
</tr>
<tr>
<td width="71">Recipe name</td>
<td width="6">:</td>
<td width="301"><input name="name" type="text" id="recipe"></td>
</tr>
<tr>
<td>Ingredients</td>
<td>:</td>
<td><input name="lastname" type="text" id="ingredients"></td>
</tr>
<tr>
<td>Instructions</td>
<td>:</td>
<td><input name="email" type="text" id="instructions"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
I would love to know if anyone knows the reason the data is invisible.
Image of table with no data

$recipe=$_POST['recipename'];
$ingredients=$_POST['ingredients'];
$instructions=$_POST['instructions'];
needs to be:
$recipe=$_POST['name'];
$ingredients=$_POST['lastname'];
$instructions=$_POST['email'];
You have the form field names wrong, so it is not getting a value and thus inserting empty data. If you set error_reporting to E_ALL you would have gotten an error. Make sure you develop with it at that setting.
Also the name attribute in the form field is what the index name would be for $_POST['INDEX_NAME'], you are mistakingly using the id attribute for that.

Related

How to update points by adding previous points and current points

How do I update my php/mysql by adding the previous vote and new vote
for example, in mysql. the vote point is 25. when I entered again with 25points. it became 50points. this is the scenario. I have table name"subj_eva" with coloumn of id, facultyname and totalvotes. how do I update my totalvotes by adding the old points and new points?
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password"; // Mysql password
$db_name="ramon_pascual"; // Database name
$tbl_name="subj_eva"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$profname=$_POST['profname'];
$votecount=$_POST['votecount'];
$subj=$_POST['subject'];
// Insert data into mysql
$sql = "UPDATE $tbl_name SET facultyname='$profname', totalvotes='$votecount', subjects='$subj'";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='indextest.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>
and this is my html code
<html>
<head><title> index test</title></head>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="welcome.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Insert Data Into mySQL Database </strong></td>
</tr>
<tr>
<td width="71">Professor Name</td>
<td width="6">:</td>
<td width="301"><input name="profname" type="text" id="profname"></td>
</tr>
<tr>
<td>vote count</td>
<td>:</td>
<td><input name="votecount" type="text" id="votecount"></td>
</tr>
<tr>
<td>subject</td>
<td>:</td>
<td><input name="subject" type="text" id="subject"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
You can modify your query to add the new value to the current value. I recommend converting votecount to an integer beforehand.
$votecount = intval($votecount);
$sql = "UPDATE $tbl_name SET facultyname='$profname', totalvotes=totalvotes + $votecount, subjects='$subj'";
Try:
$sql = "UPDATE $tbl_name SET facultyname='$profname', totalvotes=totalvotes + '$votecount', subjects='$subj'";
I do not understand what you want, but is always use in the transmission function mysql_real_escape_string() database any string variables! Otherwise possible Mysql injections. And in double quotes variables highlight the brackets {}, otherwise the function will give the database is not the variable.
Try this..
$votecount=$_POST['votecount'];
$getprevious =mysql_fetch_array(mysql_query("select * from $tbl_name order by id desc"));
$previouspoint= $getprevious[0]['totalvotes'];
$votecount = intval($previouspoint) + intval($votecount);
$sql = "UPDATE $tbl_name SET facultyname='$profname', totalvotes='$votecount', subjects='$subj'";
$result=mysql_query($sql);
try using like this
$query="update table_name set 'totalvotes'=(select `totalvotes` from `table_name` where id='".$id."')+'".$current_count."' where id='".$id."' ";

PHP UPDATE script not working

I'm trying to implement an update script on my page but it doesn't work.
I have 2 pages, the 'update.php' page and the 'update_ac.php' page that runs the script after hitting 'submit' on the Form, the code is as below:
update.php
On this page i have this error : Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\update.php on line 16
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="764503"; // Database name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$result=mysql_query("SELECT * FROM produse WHERE id='$id'");
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>titlu</strong></td>
<td align="center"><strong>stare</strong></td>
<td align="center"><strong>pret</strong></td>
<td align="center"><strong>descriere</strong></td>
</tr>
<tr>
<td> </td>
<td align="center">
<input name="titlu" type="text" id="titlu" value="<? echo $rows['titlu']; ?>">
</td>
<td align="center">
<input name="stare" type="text" id="stare" value="<? echo $rows['stare']; ?>" size="15">
</td>
<td>
<input name="pret" type="text" id="pret" value="<? echo $rows['pret']; ?>" size="15">
</td>
<td>
<input name="descriere" type="text" id="descriere" value="<? echo $rows['descriere']; ?>" size="15">
</td>
</tr>
<tr>
<td> </td>
<td>
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
update_ac.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="764503"; // Database name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE produse SET titlu='$_POST[titlu]' ,stare='$_POST[stare]' ,pret='$_POST[pret]' ,descriere='$_POST[descriere]' WHERE id='$_POST[id]'";
$result=mysql_query($sql);
// if successfully updated.
if($result)
{
echo "Successful";
}
else
{
echo "ERROR";
}
?>
mysql_query return FALSE when error occurs instead of returning an Array, this test avoid a second (and bigger) error : if FALSE no problem, if an Array you can fetch it ;oP
if ($result) $rows=mysql_fetch_array($result);
This probably means you have an error in your query. If you have an error in your query, it doesnt create a resource to get your data from.
to find an error: add or die(mysql_error()) to your mysql_query command. so you can find out the error.
You have error in your update sql query in the update_ac.php file. So nothing is probably getting updated. You are using the array indexes wrong in the following statement:
$sql="UPDATE produse SET titlu='$_POST[titlu]' ,stare='$_POST[stare]' ,pret='$_POST[pret]' ,descriere='$_POST[descriere]' WHERE id='$_POST[id]'";
$_POST[titlu] should be $_POST['titlu'], $_POST[stare] should be $_POST'stare'] and so on. So the correct query can be as below:
$sql="UPDATE produse SET titlu='".$_POST['titlu']."' ,stare='".$_POST['stare']."' ,pret='".$_POST['pret']."' ,descriere='".$_POST['descriere']."' WHERE id='".$_POST['id']."'";

php forum not detecting new line upon submission

I am trying to set up a feature very similar to a forum, where users can write chapters or stories (long sections of text). This text is then to be stored on mysql so that it can be recalled an read and commented on etc. This is all set up and works fine, however when a new line is typed by the user, it is not recognized. So the user may type -
This is para 1
This is para 2
But all that is displayed is -
This is para1This is para 2
I know that there are certain character is html and php such as or \n, but the people using the forum will not be savvy enough to do this. Is there a way that I can get this working automatically?
Below is the code for my add_topic.php
<?php
session_start();
$uname = $_SESSION['uname'];
$host="mysql.******************.co.uk"; // Host name
$username="**********"; // Mysql username
$password="************"; // Mysql password
$db_name="************_members"; // Database name
$tbl_name="forum_question"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get data that sent from form
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['username'];
$datetime=date("d/m/y h:i:s"); //create date time
$sql="INSERT INTO $tbl_name(topic, detail, name, datetime)VALUES('$topic', '$detail', '$name', '$datetime')";
$result=mysql_query($sql);
if($result){
echo "Successful<BR>";
echo "<a href=main_forum.php>View your topic</a>";
}
else {
echo "ERROR";
}
mysql_close();
?>
Currently the 'details' field on the database is set to MidText. I have also tried MidBlob but there is no change in the result.
The following is my create_topic.php code -
<?php
session_start();
$uname = $_SESSION['uname'];
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="add_topic.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3" bgcolor="#E6E6E6"><strong>Create New Story</strong> </td>
</tr>
<tr>
<td width="14%"><strong>Title</strong></td>
<td width="2%">:</td>
<td width="84%"><input name="topic" type="text" id="topic" size="50" /></td>
</tr>
<tr>
<td valign="top"><strong>Story</strong></td>
<td valign="top">:</td>
<td><textarea maxlength="2000000" name="detail" cols="50" rows="30" id="detail"></textarea></td>
</tr>
<tr>
<td><strong>Username</strong></td>
<td>:</td>
<td><input name="username" value="<?php echo $uname; ?>" type="text" id="username" size="50" readonly></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
I am an absolute beginner but I will try my best to make heads or tales of any advice that can be given. If you need anymore info please let me know.
Many thanks in advance.
Have you tried using nl2br before display? It will transform all the \n character to html <br/>.
See also the docs: http://php.net/manual/en/function.nl2br.php
Please try
$detail=htmlentities($_POST['detail']);
to get special HTML characters transformed in your database.
Hope that helps.
For further info go to http://php.net/manual/en/function.htmlentities.php

php mysql simple update: cannot read values

probably stupid question but...
I have a sample for PHP and MySQL, which doesn't work...
there are 3 php files:
list_records.php
update.php
update_ac.php
the problem is that 3rd one doesn't read variables from the 2nd. What am I doing wrong?
Here are my files/code:
list_records.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>
<tr>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['lastname']; ?></td>
<td><?php echo $rows['email']; ?></td>
<td align="center">update</td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>
update.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td> </td>
<td align="center">
<input name="name" type="text" id="name" value="<?php echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>" size="15">
</td>
<td>
<input name="email" type="text" id="email" value="<?php echo $rows['email']; ?>" size="15">
</td>
</tr>
<tr>
<td> </td>
<td>
<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
update_ac.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET name='TEST', lastname='$lastname', email='$email' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
u need to get the value in the server side from client side before inserting or updating them into the database.for this we have superglobal variables in php like $_GET,$_POST,$_REQUEST etc.
so include thses in your update_ac.php:
$lastname=$_POST[lastname];
$email=$_POST[email];
$id=$_POST[id];`
you can use $_REQUEST method if you are not sure by which method u passed the values from the form.but try to avoid it as it is less secure.
You need to get them from POST in the update_ac.php file like
$sql="UPDATE $tbl_name
SET name='TEST',
lastname='".$_POST['lastname']."',
email='".$_POST['email']."'
WHERE id=".$_POST['id'];
This is what you need in your update query
......lastname='".$_POST['lastname']."', email='".$_POST['email']."' WHERE id=".$_POST['id']
Whenever a form posts/submits a value to a file mentioned in an action, the file must access the values of form's field's name in $_POST array as $_POST['lastname'], $_POST['email']. etc. (if the method used is POST in form tag)
There are three ways of access submitted values:
GET http://php.net/manual/en/reserved.variables.get.php
POST http://php.net/manual/en/reserved.variables.post.php
REQUEST http://php.net/manual/en/reserved.variables.request.php

Updating user image using UPDATE Query ( Not working )

I am creating a user panel in which users can register and upload a image. Once they login they may decide to edit that image therefore I am wanting to perform a update query to add a new image to the location and attach the new image to their mysql data.
So whats happening when i submit the code: I get a error saying:: Sorry, there was a problem uploading your file.
Hopefully someone can let me know why this is giving me a error. PS I am already using this location when they first register to upload a file. But surely this shouldnt have any problem. Another issue i found is that nothing is being successfully inserted into the MYSQL Table for the photo.
So here is the code:
Table: admin
CREATE TABLE IF NOT EXISTS `admin` (
`id` int(3) NOT NULL auto_increment,
`UserName` varchar(30) default NULL,
`PassWord` varchar(30) default NULL,
`name` varchar(30) default NULL,
`mainContent` varchar(2000) default NULL COMMENT 'maincontent test',
`photo` varchar(400) NOT NULL COMMENT 'Picture1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=64 ;
So lets imagine the user has logged into their user panel: and they click the edit button linking to their ID.
So they appear at the edit page:
EDIT.PHP
<?php
session_start();
$UserName = $_SESSION['UserName'];
require("checkLoginSession.php");
$adminid = $_GET['id'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
echo("Logged In As: $UserName");
echo "<br />";
echo("We are editing Data for ID: $adminid");
echo "<br />";
echo "<a href=test.php>Go back to panel</a>";
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM admin WHERE id='$id'";
$result=mysql_query($sql) or die(mysql_error());
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Main Content</strong></td>
<td align="center"><strong>Image Locatoin</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="mainContent" type="text" id="mainContent" value="<? echo $rows['mainContent']; ?>" size="15"></td>
<td align="center"><input name="photo" type="file" id="photo">
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Page</title>
</head>
<body>
<h2>Edit Page (<?php echo ("$adminid"); ?>)</h2>
<img src="backtopanel.jpg" alt="back to panel" width="454" height="85" border="0" longdesc="back to panel" />
</body>
</html>
update_ac.php
<?php
session_start();
$UserName = $_SESSION['UserName'];
require("checkLoginSession.php");
include "common.php";
DBConnect();
$Link = mysql_connect($Host, $User, $Password);
$id = $_POST['id'];
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$_POST = array_map("mysql_real_escape_string", $_POST);
$firstName = $_POST["name"];
$mainText = $_POST["mainContent"];
$pic=($_FILES['photo']['name']);
//
$sql="UPDATE admin SET name='$firstName', mainContent='$mainText', photo='$pic' WHERE id='$id'";
echo $sql;
//
if (mysql_db_query ($DBName, $sql, $Link)){
print ("A record was created <br><a href=index.php> return to index </a>\n");
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
} else {
print ("Record not created");
}
mysql_close($Link);
?>
A few things:
If nothing is coming through in $_FILES then you have probably missed out enctype="multipart/form-data" in the form tag of the submitting page where the image is being specified.
When handling the uploaded file you need to use $_FILES['inputName']['tmp_name'] to get the temp location of the file then move this using move_uploaded_file($_FILES['inputName']['tmp_name'], 'destination'); and store the destination value in the DB record.
On the edit page where you want to display the image you will need to have the image itself displayed (using and img tag) as well as a file input for changing the image if required.
I could go on, but I really think you need to go read some tutorials on form / file handling in PHP as would most which is why you have not received any replies...

Categories