I have created a test form just to try to send my radio button value to mysql. I am having problems with it at the moment. The code below is just a test, I want the radio button to submit the value but it isn't.
<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>TEST </strong></td>
</tr>
<tr>
<td width="71">Name</td>
<td width="6">:</td>
<td width="301"><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>Case</td>
<td>:</td>
<td><input name="case" type="radio" id="case1"> <input name="case" type="radio" id="case2"> <input name="case" type="radio" id="case3"></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
And here is the connection part of the database
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="123"; // Mysql password
$db_name="store"; // 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 values from form
$name=$_POST['name'];
$case=$_POST['case'];
$email=$_POST['email'];
// Insert data into mysql
$sql="INSERT INTO $tbl_name(name, case, email)VALUES('$name', '$case', '$email')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>
This is done by adding values to radio button input. For instance:
<form method="post">
<input type="radio" name="sex" value="male">Male<br>
<input type="radio" name="sex" value="female">Female
<input type="submit">
</form>
You should start with closing all of your <input> tags with </input> or at least a slash at the end (like <input name="case" type="radio" id="case1"></input>).
You should set values to your radios (like this they always return 'on'), whereas the submit button needs neither name nor value.
EDIT:
Define a default radio with selected in yout input tag! If none is selected, there's no case getting transmitted and PHP will throw Undefined index: case when accessing $_POST['case'].
A good way to prevent such errors is to check if all necessary indices are set. You can do the following:
if(isset($_POST['name']) and isset($_POST['case']) and isset($_POST['email'])) { ... }
<!-- Once you have created Mysql connection and column in specified database table,-->
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "website";
// Create connection
$conn = mysqli_connect($servername, $username, $password,$dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
else{
echo "Connected successfully";}
$sql="INSERT INTO Registration(Name,FatherName,CNIC,FCNIC,Email,Password,Contact,Gender) VALUES ('$_POST[Name]','$_POST[FatherName]','$_POST[CNIC]','$_POST[FCNIC]','$_POST[Email]','$_POST[Password]','$_POST[Contact]','$_POST[Gender]')";
if (!mysqli_query($conn,$sql))
{
die('Error:'.mysqli_error($conn));
}
echo " & 1 record added";
mysqli_close($conn);
?>
<!--after that you just need to write this code and make sure to adjust this code because i'm posting some portion of my code."-->
<h4>Gender</h4> <input type="radio" value="Male" name="Gender"> Male
<input type="radio" value="Female" name="Gender" >Female <br><br>
<button type="Submit">Submit</button><br>
//This answer is for you.
Related
I try to use the below code to update multiple rows, the below code can view the results of rows but it can not be updated, where is wrong place? How to modify it ?
<?php
$host="localhost"; // Host name
$username="abc"; // Mysql username
$password="abc123"; // Mysql password
$db_name="abc"; // Database name
$tbl_name="BRAddress"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("Cannot connect");
mysql_select_db("$db_name")or die("Cannot select Database");
$sql="SELECT * FROM $tbl_name WHERE br_no='62779457'";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>BR No.</strong></td>
<td align="center"><strong>Date of Register</strong></td>
<td align="center"><strong>Address</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center">
<? $br_no[]=$rows['br_no']; ?><? echo $rows['br_no']; ?>
</td>
<td align="center">
<input name="br_date_of_register[]" type="date" id="br_date_of_register" value="<? echo $rows['br_date_of_register']; ?>">
</td>
<td align="center">
<input name="br_address[]" type="text" size="60" id="br_address" value="<? echo $rows['br_address']; ?>">
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET
br_date_of_register='$br_date_of_register[$i]',
br_address='$br_address[$i]'
WHERE br_no='$br_no[$i]'";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:update_sample.php");
}
mysql_close();
?>
Thank you very much for your help & support !
I think that you need to change this part
if($Submit){
to
if($_POST('Submit')){
I haven't run the whole or looked at entire code, but you have nothing that defines $Submit variable though from what I see.
Or you can put in
$Submit = $_POST('Submit');
before the if statement.
Let me know how you go.
Cheers
Your POST variable are empty. $br_date_of_register has no value. You must use this like following
$br_date_of_register = $_POST[br_date_of_register];
$br_address = $_POST[br_address];
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET
br_date_of_register='$br_date_of_register[$i]',
br_address='$br_address[$i]'
WHERE br_no='$br_no[$i]'";
$result1=mysql_query($sql1);
}
Edit
if($Submit)
To
if($_SERVER['REQUEST_METHOD'] == "POST")
Considering your Submit check,
you can use this,
if(isset($_POST["Submit"]))
{
}
Further in your SQL statement, do this,
$sql1='UPDATE ' . $tbl_name . ' SET
br_date_of_register = ' . $br_date_of_register[$i] .
' , br_address = ' . $br_address[$i] .
' WHERE br_no = ' . $br_no[$i];
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']."'";
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
I have 2 PHP files for this. The first one update.php contains the user form to update the row. The next one,update_ac contains the coding to carry out this update. The problem is i do not get a proper output
<?php
$host = "localhost"; // Host name
$username = "root"; // Mysql username
$password = ""; // Mysql password
$db_name = "yumyum"; // Database name
$tbl_name = "food"; // 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'];
$id = $_REQUEST['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 align="center"><strong>name</strong></td>
<td align="center"><strong>price</strong></td>
<td align="center"><strong>Quantity</strong></td>
</tr>
<td align="center">
<input name="name" type="text" id="name" value="<? echo $line['name']; ?>">
</td>
<td align="center">
<input name="price" type="text" id="price" value="<? echo $line['price']; ?>" size="15">
</td>
<td align="center">
<input name="Quantity" type="text" id="Quantity" value="<? echo $line['Quantity']; ?>" size="15">
</td>
<td>
<input name="id" type="hidden" id="id" value="<? echo $line['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
**This is update_ac.php**
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="yumyum"; // Database name
$tbl_name="food"; // 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='$name', price='$price', Quantity='$Quantity' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='yumhome.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
At First
change the variable $lines to $rows
Then in update_ac.php
get all the value using $_POST[];
because in form method="post"
I'am trying to do very simple php page and have some kind of problem here. the page works and everything but there is nothing coming to mysql. When I press submit it prints insert into DrinkHistory(CustomerId,DrinkId,SellerId) values('1','3','2')Your Data Inserted. But when I login to phpmyadmin there is nothing in DrinkHistory table.
the code is below
<?php
// Create connection
$con=mysqli_connect("localhost","******","*******","test");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Mene kotii ku et mitää osaa: " . mysqli_connect_error();
}
// Insert Data
#$a=$_POST['CustomerId'];
#$b=$_POST['DrinkId'];
#$c=$_POST['SellerId'];
if(#$_POST['submit'])
{
echo $s="insert into DrinkHistory(CustomerId,DrinkId,SellerId) values('$a','$b','$c')";
echo "Your Data Inserted";
mysql_query($s);
}
?>
<center>
<form method="post">
<table width="100%" height="245" border="1" bgcolor="#00CCFF">
<tr><td width="112" height="26">Asiakas</td>
<td width="100"><input type="radio" name="CustomerId" value="1"/>Jokirinne Niko</td>
</tr>
<tr><td rowspan="2">Juoma</td>
<form method)="post">
<td height="28"><input type="radio" name="DrinkId" value="2"/>Kalja</td>
<td height="28"><input type="radio" name="DrinkId" value="3"/>Lonkero</td>
<td height="28"><input type="radio" name="DrinkId" value="4"/>Siideri</td>
<td height="28"><input type="radio" name="DrinkId" value="5"/>Fisu</td>
<td height="28"><input type="radio" name="DrinkId" value="6"/>Tequila</td>
<td height="26"><input type="radio" name="DrinkId" value="7"/>MustikkaShotti</td>
</tr>
<td height="33"></tr>
<tr><td rowspan="3">Myyjä</td>
<td><input type="radio" name="SellerId" value="1"/>Niko Jokirinne</td>
<tr>
<td><input type="radio" name="SellerId" value="2"/>Tanya Lickorish</td>
<tr>
<tr><td height="62"><input type="submit" name="submit" value="Juo"/></td></tr>
</table>
</form>
</center>
</body>
</html>
You seem to be omitting the connection string (using mysql_query instead of mysqli_query) and error verification. Try this:
<?php
// Create connection
$con=mysqli_connect("localhost","******","*******","test");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Mene kotii ku et mitää osaa: " . mysqli_connect_error();
}
// Insert Data
#$a=$_POST['CustomerId'];
#$b=$_POST['DrinkId'];
#$c=$_POST['SellerId'];
if(#$_POST['submit']) {
echo $s = "insert into DrinkHistory(CustomerId,DrinkId,SellerId) values('$a','$b','$c')";
if (mysqli_query($con, $s)) {
echo "Your Data Inserted";
} else {
echo "Error inserting data: ".mysqli_error();
}
}
?>
You have several database libraries to choose from but, once you decide, you need to stick to one. You cannot start a connection with the new mysqli extension:
$con=mysqli_connect("localhost","******","*******","test");
... and then try to query the database with the legacy deprecated functions:
mysql_query($s);
If you are not getting a blattant error message on screen you've probably failed to configure PHP to display error messages in your development box.That's something you need to fix before you go further; it's impossible to code without the aid of error messages. Here's a brief explanation.
Finally, you verify if your connection fails:
if (mysqli_connect_errno($con))
... but you don't test if your query fails.