SQL Insert Issue with PHP - php

I have the following code to insert data into a DB but when inserting the data some times it insert successfully while most of the time its giving me error like below.
Could not enter data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's fist name?', 'Kabul Janm', 'Afghanistan', 'Kabul', 'Kabul', '1985-03-26', 'Mal' at line 1
Can someone help me out, I need to have it stable, the code is as below please,
<html>
<head>
<title>Add New Record in MySQL Database</title>
<script src="SpryAssets/SpryCollapsiblePanel.js" type="text/javascript"></script>
<script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>
<link href="SpryAssets/SpryCollapsiblePanel.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
include_once ('top.php');
?>
<?php
include '/Connections/conn.php';
if(isset($_POST['add']))
{
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if(! get_magic_quotes_gpc() )
{
$first_name = addslashes ($_POST['first_name']);
$last_name = addslashes ($_POST['last_name']);
}
else
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
}
$email_address = $_POST['email_address'];
$phone_no = $_POST['Phone_no'];
$user_name = $_POST['user_name'];
$password = $_POST['password'];
$sec_question = $_POST['sec_question'];
$Answer = $_POST['Answer'];
$Country = $_POST['Country'];
$State = $_POST['State'];
$city = $_POST['city'];
$date_birth = $_POST['date_birth'];
$gender = $_POST['gender'];
$sql = "INSERT INTO users(first_name, last_name, email_address, Phone_no, user_name, password, sec_question, Answer, Country, State, city, date_birth, gender) VALUES('$first_name', '$last_name', '$email_address', '$phone_no', '$user_name', '$password', '$sec_question', '$Answer', '$Country', '$State', '$city', '$date_birth', '$gender')";
$dbname;
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
header("Location: /thank.php");
//echo "<center>Thanks for registration in Mashwani Info Tech Free Online Trainings (MOFT)</center>\n";
mysql_close($conn);
}
else
{
?>
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<link href="SpryAssets/SpryValidationPassword.css" rel="stylesheet" type="text/css" />
<link href="SpryAssets/SpryValidationConfirm.css" rel="stylesheet" type="text/css" />
<link href="SpryAssets/SpryValidationSelect.css" rel="stylesheet" type="text/css" />
<script src="SpryAssets/SpryValidationPassword.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationConfirm.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationSelect.js" type="text/javascript"></script>
<table width="100%" background="/Images/gradient_medium.jpg">
<tr>
<td width="100%" height="34">
<!--<center> <marquee behavior="Scroll" width="100%" scrollamount="8" direction="Right"><img src="/Images/mtn.jpg" /> <img src="/Images/mtn1.jpg" /></marquee> </center>
-->
</td>
</tr>
</table>
<table width="100%" align="center" bgcolor="#ECF5F0" border="0">
<tr valign="bottom"> <td height="25"><p> </p>
<form action="<?php $_PHP_SELF?>" method="post" name="form1" id="form1">
<table align="center" border="1">
<tr valign="baseline">
<td colspan="2" align="left" nowrap="nowrap" bordercolor="#CCCC33">First Name</td>
<td width="388"><span id="sprytextfield1">
<input type="text" name="first_name" value="" size="37" id="first_name"/>
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr valign="baseline">
<td colspan="2" align="left" nowrap="nowrap">Last Name</td>
<td><input type="text" name="last_name" id="last_name" value="" size="37" /></td>
</tr>
<tr valign="baseline">
<td colspan="2" align="left" nowrap="nowrap">Email Address</td>
<td><span id="sprytextfield2">
<input type="text" name="email_address" id="email_address" value="" size="37" />
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
</tr>
<tr valign="baseline">
<td colspan="2" align="left" nowrap="nowrap">Phone No <font size="-4" color="#00CC66">(0093772221521)</font></td>
<td><span id="sprytextfield3">
<input type="text" name="Phone_no" id="Phone_no" value="" size="37" />
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
</tr>
<tr valign="baseline">
<td colspan="2" align="left" nowrap="nowrap">User Name</td>
<td><span id="sprytextfield4">
<input type="text" name="user_name" id="user_name" value="" size="37" />
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr valign="baseline">
<td colspan="2" align="left" nowrap="nowrap">Password <font size="-4" color="#00CC66">(Min 8 Charectors) </font></td>
<td><span id="pass">
<input type="password" name="password" value="" size="37" id="password" />
<span class="passwordRequiredMsg">A value is required.</span><span class="passwordInvalidStrengthMsg">The password doesn't meet the specified strength.</span></span></td>
</tr>
<tr valign="baseline">
<td colspan="2" align="left" nowrap="nowrap">Confirm Password:</td>
<td><span id="spryconfirm1">
<label for="confirm"></label>
<input name="confirm" type="password" id="confirm" size="37" />
<span class="confirmRequiredMsg">A value is required.</span><span class="confirmInvalidMsg">The values don't match.</span></span></td>
</tr>
<tr valign="baseline">
<td colspan="2" align="left" nowrap="nowrap">Secret Question</td>
<td><span id="spryselect1">
<label for="sec"></label>
<select name="sec_question" id="sec_question">
<option value="What is your fist school name?">What is your fist school name?</option>
<option value="Where did your birth happened?">Where did your birth happened?</option>
<option value="What is your father's fist name?">What is your father's fist name?</option>
<option value="Where did you get your degree?">Where did you get your degree?</option>
</select>
<span class="selectRequiredMsg">Please select an item.</span></span></td>
</tr>
<tr valign="baseline">
<td colspan="2" align="left" nowrap="nowrap">Answer for Question</td>
<td><span id="sprytextfield5">
<input type="text" name="Answer" id="Answer" value="" size="37" />
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr valign="baseline">
<td width="61" rowspan="3" align="left" valign="middle" nowrap="nowrap">Address</td>
<td width="121" align="left" nowrap="nowrap">Country</td>
<td><input type="text" name="Country" id="Country" value="" size="37" /></td>
</tr>
<tr valign="baseline">
<td width="121" align="left" nowrap="nowrap">State</td>
<td><span id="sprytextfield6">
<input type="text" name="State" id="State" value="" size="37" />
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr valign="baseline">
<td width="121" align="left" nowrap="nowrap">City</td>
<td><span id="sprytextfield7">
<input type="text" name="city" id="city" value="" size="37" />
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr valign="baseline">
<td colspan="2" align="left" nowrap="nowrap">Date of Birth <font size="-4" color="#00CC66"> (YYYY-MM-DD) </font></td>
<td><span id="sprytextfield8">
<input type="text" name="date_birth" id="date_birth" value="" size="37" />
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
</tr>
<tr valign="baseline">
<td colspan="2" align="left" nowrap="nowrap">Gender</td>
<td><span id="spryselect2">
<label for="gen"></label>
<select name="gender" id="gender">
<option value="Select your gender here." selected="selected">Select your gender here.</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<span class="selectRequiredMsg">Please select an item.</span></span></td>
</tr>
</table>
<center> <input name="add" type="submit" value="Sign Up" id="add" /> </center>
<input type="hidden" name="ID" value="" />
<input type="hidden" name="admin_level" id="admin_level" value="" />
<input type="hidden" name="time_stamp" id="time_stamp" value="" />
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p> </p></td>
</tr>
</table>
<?php
}
?>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "none", {validateOn:["blur"]});
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "email");
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "phone_number", {format:"phone_custom"});
var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4");
var sprypassword1 = new Spry.Widget.ValidationPassword("pass", {minAlphaChars:1, minUpperAlphaChars:1, minSpecialChars:1, validateOn:["blur"]});
var spryconfirm1 = new Spry.Widget.ValidationConfirm("spryconfirm1", "password", {validateOn:["blur"]});
var spryselect1 = new Spry.Widget.ValidationSelect("spryselect1");
var sprytextfield5 = new Spry.Widget.ValidationTextField("sprytextfield5");
var sprytextfield6 = new Spry.Widget.ValidationTextField("sprytextfield6");
var sprytextfield7 = new Spry.Widget.ValidationTextField("sprytextfield7");
var sprytextfield8 = new Spry.Widget.ValidationTextField("sprytextfield8", "date", {format:"yyyy-mm-dd"});
var spryselect2 = new Spry.Widget.ValidationSelect("spryselect2");
var CollapsiblePanel1 = new Spry.Widget.CollapsiblePanel("CollapsiblePanel1");
</script>
<?php
include_once ('bottom.php');
?>
</body>
</html>

You have to escape "'" signs before using data in a query.
What happens is that your users enter "'" in the input field, and MySQL gets an error.
Use prepared statements for automatical way to deal with that problem.
Here is and example.
Edit:
Edit your code like this:
if(! get_magic_quotes_gpc() )
{
$first_name = addslashes ($_POST['first_name']);
$last_name = addslashes ($_POST['last_name']);
$email_address = addslashes ($_POST['email_address']);
$phone_no = addslashes ($_POST['Phone_no']);
$user_name = addslashes ($_POST['user_name']);
$password = addslashes ($_POST['password']);
$sec_question = addslashes ($_POST['sec_question']);
$Answer = addslashes ($_POST['Answer']);
$Country = addslashes ($_POST['Country']);
$State = addslashes ($_POST['State']);
$city = addslashes ($_POST['city']);
$date_birth = addslashes ($_POST['date_birth']);
$gender = addslashes ($_POST['gender']);
}
else
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];
$phone_no = $_POST['Phone_no'];
$user_name = $_POST['user_name'];
$password = $_POST['password'];
$sec_question = $_POST['sec_question'];
$Answer = $_POST['Answer'];
$Country = $_POST['Country'];
$State = $_POST['State'];
$city = $_POST['city'];
$date_birth = $_POST['date_birth'];
$gender = $_POST['gender'];
}
It should prevent the errors.

Related

How to use nl2br to fetch data of textarea using php?

I am new in php. I have a web form. In which i use textarea. I have another PHP page where I display data from db and applying CRUD operation. Now here is the problem when I click on Edit button my all form data has been fetched from db but the formatting of textarea Text has been changed. It shows \r\r\r\n I use nl2br but its not working. I want to display my data in same formating.
<textarea rows="20" cols="100" id="text" name="text" style="font-size:14px;" > <?php echo !empty(nl2br($text))?(nl2br($text)):'';?> </textarea><td>
My Full Page Code is
<?php
require 'database.php';
$id = null;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if ( null==$id ) {
header("Location: index.php");
}
if ( !empty($_POST)) {
// keep track post values
$file_name = $_POST['file_name'];
$ref_no = $_POST['ref_no'];
$to_name = $_POST['to_name'];
$confidential = $_POST['confidential'];
$designation = $_POST['designation'];
$date = $_POST['date'];
$solutation = $_POST['solutation'];
$entity = $_POST['entity'];
$add_1 = $_POST['add_1'];
$thank_you = $_POST['thank_you'];
$add_2 = $_POST['add_2'];
$yours_truly = $_POST['yours_truly'];
$add_3 = $_POST['add_3'];
$sign_name = $_POST['sign_name'];
$city = $_POST['city'];
$s_designation = $_POST['s_designation'];
$heading_line_1 = $_POST['heading_line_1'];
$encl_line_1 = $_POST['encl_line_1'];
$heading_line_2 = $_POST['heading_line_2'];
$encl_line_2 = $_POST['encl_line_2'];
$heading_line_3 = $_POST['heading_line_3'];
$encl_line_3 = $_POST['encl_line_3'];
$text = mysql_real_escape_string( $_POST['text'] );
// update data
$valid = true;
if ($valid) {
$pdo = Database::connect();
// $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE test set file_name ='$file_name', ref_no ='$ref_no', to_name ='$to_name',
confidential ='$confidential', designation = '$designation', date ='$date',
solutation ='$solutation', entity ='$entity', add_1 ='$add_1',
thank_you ='$thank_you', add_2 ='$add_2', yours_truly ='$yours_truly',
add_3 ='$add_3', sign_name ='$sign_name', city ='$city',
s_designation ='$s_designation', heading_line_1 ='$heading_line_1', encl_line_1 ='$encl_line_1',
heading_line_2 ='$heading_line_2', encl_line_2 ='$encl_line_2', heading_line_3 ='$heading_line_3',
encl_line_3 ='$encl_line_3', text ='$text' WHERE id ='$id'";
$q = $pdo->prepare($sql);
$q->execute(array($file_name,$ref_no,$to_name,$confidential,$designation,$date,$solutation,$entity,$add_1,
$thank_you,$add_2,$yours_truly,$add_3,$sign_name,$city,$s_designation,$heading_line_1,$encl_line_1,$heading_line_2,
$encl_line_2,$heading_line_3,$encl_line_3,$id));
Database::disconnect();
header("Location: index.php");
}
else {
}
}
$con=mysqli_connect("localhost","MY_LOGIN","MY_PASSWORD","MY_DATABASE");
$id2 = $_GET['id'];
$sql = "SELECT * FROM test where id='$id2'";
$result=mysqli_query($con,$sql);
$row= (mysqli_fetch_array($result,MYSQLI_ASSOC));
$file_name = $row['file_name'];
$ref_no = $row['ref_no'];
$to_name = $row['to_name'];
$confidential = $row['confidential'];
$designation = $row['designation'];
$date = $row['date'];
$solutation = $row['solutation'];
$entity = $row['entity'];
$add_1 = $row['add_1'];
$thank_you = $row['thank_you'];
$add_2 = $row['add_2'];
$yours_truly = $row['yours_truly'];
$add_3 = $row['add_3'];
$sign_name = $row['sign_name'];
$city = $row['city'];
$s_designation = $row['s_designation'];
$heading_line_1 = $row['heading_line_1'];
$encl_line_1 = $row['encl_line_1'];
$heading_line_2 = $row['heading_line_2'];
$encl_line_2 = $row['encl_line_2'];
$heading_line_3 = $row['heading_line_3'];
$encl_line_3 = $row['encl_line_3'];
$text = mysql_real_escape_string( $row['text'] );
?>
<!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>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="newstyles.css">
<script src="js/bootstrap.min.js"></script>
<script>
function show_confirm(){
return confirm("Copy is being created....");
window.location.href='index.php';
}
</script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$("#date").datepicker({
dateFormat: "yy-mm-dd"
}).datepicker();
});
</script>
</head>
<body>
<form action="edit4.php?id=<?php echo $id; ?>" method="POST" >
<table border="0" class="DivTableBorder" width="840px">
<tr>
<td class="DivSubHeaderCellTop" colspan="6">Letters</td>
</tr> <tr> </td> </tr>
<tr>
<td class="DivCellText" width="80px">File Name </td>
<td class="DivCellText" width="480px" colspan="3"><input name="file_name" type="text" id="file_name"
value="<?php echo !empty($file_name)?$file_name:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">Referance #</td>
<td class="DivCellText" width="200px"><input name="ref_no" type="text" id="ref_no"
value="<?php echo !empty($ref_no)?$ref_no:'';?>" class="inputRemarks" />
</td> </tr>
<tr ><td bgcolor="#999999" colspan="4"></td>
<td class="DivCellText" width="80px"></td>
<td class="DivCellText" width="200px"></td>
</tr>
<tr>
<td class="DivCellText" width="80px">To - Name</td>
<td class="DivCellText" colspan="3"><input name="to_name" type="text" id="to_name"
value="<?php echo !empty($to_name)?$to_name:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">Confidential</td>
<td class="DivCellText" width="200px">
<?php if($confidential == "on"){ ?>
<input name="confidential" type="checkbox" checked="checked" id="confidential" value="on" />
<?php }else{ ?>
<input name="confidential" type="checkbox" id="confidential" value="on" />
<?php } ?>
</td> </tr>
<tr>
<td class="DivCellText" width="80px"> Designation</td>
<td class="DivCellText" colspan="3"><input name="designation" type="text" id="designation"
value="<?php echo !empty($designation)?$designation:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">Date :</td>
<td class="DivCellText" width="200px">
<input name="date" type="text" id="date" value="<?php echo $date; ?>" />
</td> </tr>
<tr>
<td class="DivCellText" > </td>
<td class="DivCellText" colspan="3"> </td>
<td class="DivCellText" width="80px">Solutation</td>
<td class="DivCellText" width="200px" >
<select name='solutation' id='solutation' size='1' STYLE='width: 95%' value="<?php echo !empty($solutation)?$solutation:'';?>" >
<option value='Others' >[--Others--]</option>
<option value='Dear Sir' >Dear Sir</option>
<option value='Madam' >Madam</option>
</select>
</td> </tr>
<tr>
<td class="DivCellText" width="80px"> Entity</td>
<td class="DivCellText" colspan="3"><input name="entity" type="text" id="entity"
value="<?php echo !empty($entity)?$entity:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px"> </td>
<td class="DivCellText" width="200px" >
<input name="txtSolutation" type="text" id="txtSolutation"
value="" class="inputRemarks" />
</td> </tr>
<tr>
<td class="DivCellText" width="80px"> Add-1</td>
<td class="DivCellText" colspan="3"><input name="add_1" type="text" id="add_1"
value="<?php echo !empty($add_1)?$add_1:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">Thank You.</td>
<td class="DivCellText" width="200px" ><input name="thank_you" type="text" id="thank_you"
value="<?php echo !empty($thank_you)?$thank_you:'';?>" class="inputRemarks" />
</td>
</tr>
<tr>
<td class="DivCellText" width="80px"> Add-2</td>
<td class="DivCellText" colspan="3"><input name="add_2" type="text" id="add_2"
value="<?php echo !empty($add_2)?$add_2:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">Yours truly</td>
<td class="DivCellText" width="200px" >
<select name='yours_truly' id='yours_truly' size='1' STYLE='width: 95%' value="<?php echo !empty($yours_truly)?$yours_truly:'';?>" >
<option value='1' >Yours truly</option>
<option value='2' >Regards</option>
</select>
</td> </tr>
<tr>
<td class="DivCellText" width="80px"> Add-3</td>
<td class="DivCellText" colspan="3"><input name="add_3" type="text" id="add_3"
value="<?php echo !empty($add_3)?$add_3:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">Signature-Name</td>
<td class="DivCellText" width="200px" >
<select name='sign_name' id='sign_name' size='1' style='width:95%' value="<?php echo !empty($sign_name)?$sign_name:'';?>">
<option value='1' >Adnan Afaq</option>
<option value='2' >Muhammad Shahzad Saleem</option>
<option value='3' >Adnan Dilawar</option>
<option value='4' >Rana Muhammad Nadeem</option>
<option value='5' >Jhangeer Hanif</option>
</select>
</td> </tr>
<tr>
<td class="DivCellText" width="80px"> City</td>
<td class="DivCellText" colspan="3"><input name="city" type="text" id="city"
value="<?php echo !empty($city)?$city:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">S-Designation</td>
<td class="DivCellText" width="200px">
<select name='s_designation' id='s_designation' size='1' STYLE='width: 95%' value="<?php echo !empty($s_designation)?$s_designation:'';?>" >
<option value='1' >Managing Director</option>
<option value='2' >Chief Operating Officer</option>
<option value='3' >Manager Ratings</option>
<option value='4' >Unit Head Ratings</option>
</select>
</td>
</tr>
<tr>
<td class="DivCellText" width="80px">Heading Line-1</td>
<td class="DivCellText" width="480px" colspan="3"><input name="heading_line_1" type="text" id="heading_line_1"
value="<?php echo !empty($heading_line_1)?$heading_line_1:'';?>" class="inputRemarks" maxlength="55"/>
</td>
<td class="DivCellText" width="80px">Encl: Line-1</td>
<td class="DivCellText" width="200px" >
<input name="encl_line_1" type="text" id="encl_line_1" value="<?php echo !empty($encl_line_1)?$encl_line_1:'';?>" class="inputRemarks" />
</td>
</tr>
<tr>
<td class="DivCellText" width="80px">Heading Line-2</td>
<td class="DivCellText" width="480px" colspan="3"><input name="heading_line_2" type="text" id="heading_line_2"
value="<?php echo !empty($heading_line_2)?$heading_line_2:'';?>" class="inputRemarks" maxlength="55" />
</td>
<td class="DivCellText" width="80px"> Line-2</td>
<td class="DivCellText" width="200px" >
<input name="encl_line_2" type="text" id="encl_line_2" value="<?php echo !empty($encl_line_2)?$encl_line_2:'';?>" class="inputRemarks" />
</tr>
<tr>
<td class="DivCellText" width="80px">Heading Line-3</td>
<td class="DivCellText" width="480px" colspan="3"><input name="heading_line_3" type="text" id="heading_line_3"
value="<?php echo !empty($heading_line_3)?$heading_line_3:'';?>" class="inputRemarks" maxlength="55" />
</td>
<td class="DivCellText" width="80px"> Line-3</td>
<td class="DivCellText" width="200px">
<input name="encl_line_3" type="text" id="encl_line_3" value="<?php echo !empty($encl_line_3)?$encl_line_3:'';?>" class="inputRemarks" />
</td>
</tr>
<tr ><td bgcolor="#999999" colspan="6"></td></tr>
<tr ><td colspan="6">
<table border="0" class="DivTableBorder" width="840px">
<tr>
<td class="DivCellText" colspan="4">
<textarea rows="20" cols="100" id="text" name="text" style="font-size:14px;" > <?php echo !empty(nl2br($text))?(nl2br($text)):'';?> </textarea><td>
</tr>
<tr>
<td width="100"><input type="submit" name="submit" value="Create Copy" onclick="return show_confirm();" class="blueButton"></input></td>
<td width="100"><input type="reset" name="reset" value="Cancel" class="blueButton" /> </td>
<td width="100">
<input type="submit" name="submit" value="Save" class="blueButton"></input>
</td>
<td width="303"> <input type="submit" name="submit" value="Update" class="blueButton"> </input> </td>
<td width="209">
<a class="btn" href="index.php">Back</a> </td>
</tr>
</table>
</form>
<?php
$_POST['submit']="";
if($_POST['submit'] == "Create Copy"){
$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
$db = mysql_select_db("pacra1", $connection); // Selecting Database from Server
if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$file_name = $_POST['file_name'];
$ref_no = $_POST['ref_no'];
$to_name = $_POST['to_name'];
$confidential = $_POST['confidential'];
$designation = $_POST['designation'];
$date = $_POST['date'];
$solutation = $_POST['solutation'];
$entity = $_POST['entity'];
$add_1 = $_POST['add_1'];
$thank_you = $_POST['thank_you'];
$add_2 = $_POST['add_2'];
$yours_truly = $_POST['yours_truly'];
$add_3 = $_POST['add_3'];
$sign_name = $_POST['sign_name'];
$city = $_POST['city'];
$s_designation = $_POST['s_designation'];
$heading_line_1 = $_POST['heading_line_1'];
$encl_line_1 = $_POST['encl_line_1'];
$heading_line_2 = $_POST['heading_line_2'];
$encl_line_2 = $_POST['encl_line_2'];
$heading_line_3 = $_POST['heading_line_3'];
$encl_line_3 = $_POST['encl_line_3'];
$text = mysql_real_escape_string( $_POST['text'] );
//$txtTitle = mysql_real_escape_string( $_POST['txtTitle'] );
//$txtRational = $_POST['txtRational'];
//Insert Query of SQL
$query = mysql_query("INSERT INTO test(file_name, ref_no, to_name, confidential, designation, date, solutation, entity, add_1, thank_you, add_2, yours_truly, add_3, sign_name, city, s_designation, heading_line_1, encl_line_1, heading_line_2, encl_line_2, heading_line_3, encl_line_3, text)
values
('$file_name', '$ref_no', '$to_name', '$confidential', '$designation', '$date', '$solutation', '$entity', '$add_1', '$thank_you', '$add_2', '$yours_truly', '$add_3', '$sign_name', '$city', '$s_designation', '$heading_line_1', '$encl_line_1', '$heading_line_2', '$encl_line_2', '$heading_line_3', '$encl_line_3', '$text')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
//mysql_close($connection); // Closing Connection with Server
}
?>
</body>
</html>
You should change your php echo code as you dont need to include the !empty verification. If you do then you will have to create that before you echo the textbox. I suggest you change your code to this:
<textarea rows="20" cols="100" id="text" name="text" style="font-size:14px;" > <?php echo nl2br($text);?> </textarea><td>
You can also find some documentation of the nl2br here :
http://php.net/manual/en/function.nl2br.php
Upon inserting to the database, apply the nl2br - as you are likely to be presenting that data as text on a page more often than an textarea.
$mysql->real_escape_string(trim(nl2br($_POST['text'])))
Where $mysql is the mysqli database connection
Disclaimer As Bartdude mentioned, this is not especially good practice, since the information in the database should be as free from tags as possible - however, if this data is to be used in HTML pages only, then this solution should work.
Then when returning the data to a textarea, simply remove the <br> tags, the newline characters will be there anyway and should be interpreted by the browser correctly.
str_replace("<br>", " ", $row['text'])

how to enter data enter by user in form in mysql database

I have already created database and table in it.But it is showing an error "input data failed"
the code of reg form is as follow
Code of form login2.php:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Form</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen, projection" />
</head>
<body>
<div id="div-regForm">
<div class="form-title" align ="center">VIDEO CHAT </div>
<br></br>
<div class="form-sub-title" align="center">If already registered</div>
<P align="center">
<td><input type="submit" class="blueButton" value="Sign in" align="right"/>
</P>
<br></br>
<div class="form-title">For New User</div>
<div class="form-title">Sign Up</div>
<div class="form-sub-title">It's free and anyone can join</div>
<form id="regForm" action="validate_login.php" method="post">
<table>
<tbody>
<tr>
<td><label for="fname">First Name:</label></td>
<td><div class="input-container">
<input name="fname" id="fname" type="text" />
</div></td>
</tr>
<tr>
<td><label for="lname">Last Name:</label></td>
<td><div class="input-container">
<input name="lname" id="lname" type="text" />
</div></td>
</tr>
<tr>
<td><label for="email">Your Email:</label></td>
<td><div class="input-container">
<input name="email" id="email" type="text" />
</div></td>
</tr>
<tr>
<td><label for="npass">New Password:</label></td>
<td><div class="input-container">
<input name="npass" id="npass" type="password" />
</div></td>
</tr>
<tr>
<td><label for="cpass">Confirm Password:</label></td>
<td><div class="input-container">
<input name="cpass" id="cpass" type="password" />
</div></td>
</tr>
<tr>
<td><label for="sex-select">I am:</label></td>
<td>
<div class="input-container">
<select name="sex-select" id="sex-select">
<option value="0">Select Sex:</option>
<option value="1">Female</option>
<option value="2">Male</option>
</select>
</div>
</td>
</tr>
<td>
<div class="input-container">
<tr>
<td> </td>
<td><input type="submit" class="greenButton" value="Sign Up" value="Sent" />
<img id="loading" src="img/ajax-loader.gif" alt="working.." />
</td>
</tr>
</tbody>
</table>
</form>
<div id="error">
</div>
</div>
</body>
</html>
to enter data in database code validate_login.php:
`
<?php
// Grab User submitted information
$db_host= "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "testdb";
#mysql_connect("$db_host", "$db_user", "$db_pass")
or die("Could not connect to MySQL server!");
/*#mysql_select_db("$db_name") or die("No DATABASE");
echo"hi how r u";*/
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$email = $_POST["email"];
$npass = $_POST["npass"];
$cpass = $_POST["cpass"];
$gender = $_POST["sex-select"];
// Select the database to use
#mysql_select_db("testdb")
or die("Could not select database!");
$order = "Insert INTO 'data_user'
(fname, lname, email, npass, cpass, sex-select)
VALUES
('$fname',
'$lname','$email','$npass', '$cpass','$gender')";
$result = mysql_query($order); //order executes
if($result){
echo("<br>Input data is succeed");
} else{
echo("<br>Input data is fail");
}
?>
tell me the solution asap
Step1:
Please change the column name as sex-select to sex_select
Step2:
And remove quotes in table name 'data_user' to data_user

Saving runtime created table rows using PHP and MySQL

I am new to PHP. I am working on a form which has a table with thee columns that is two textboxes and and one combo box. I used JavaScript for generating the table rows and the same to delete it.
I also referred one of your blogs for generating the rows. Now I am stuck on how to save the values from the the generated textbox to the MySQL database. Can anyone help me out? It would be a huge favor.
Here is my code:
<?php
session_start();
if (!isset($_SESSION["username"])) {
header("Location: unauthorize_access.php");
}
if(isset($_POST['submit']))
{
$length = count($_POST['testparameters']);
for($i=0; $i< $length; $i++){
$TestParameters=$_POST['testparameters'][$i];
if(!empty($TestParameters)){
$InsertQuery = "INSERT INTO color(name) VALUES('$TestParameters')";
$result=mysql_query($InsertQuery) or die(mysql_error());
}
}
if(!$InsertQuery)
{
die('Could not enter data:' . mysql_error());
}
}
?>
<!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>Makhtab</title>
<link rel="stylesheet" type="text/css" href="form2/view.css" media="all">
<script type="text/javascript" src="form2/view.js"></script>
<script type="text/javascript" src="form2/calendar.js"></script>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function deleteRow(row)
{
var i=row.parentNode.parentNode.rowIndex;
document.getElementById('POITable').deleteRow(i);
}
function insRow()
{
console.log( 'hi');
var x=document.getElementById('POITable');
var new_row = x.rows[1].cloneNode(true);
var len = x.rows.length;
new_row.cells[0].innerHTML = len;
var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
inp1.id += len;
inp1.value = '';
var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
inp2.id += len;
inp2.value = '';
x.appendChild( new_row );
}
</script>
<script type="text/javascript">
function deletezimRow(row)
{
var i=row.parentNode.parentNode.rowIndex;
document.getElementById('zimtable').deleteRow(i);
}
function inszimRow()
{
console.log( 'hi');
var x=document.getElementById('zimtable');
var new_row = x.rows[1].cloneNode(true);
var len = x.rows.length;
new_row.cells[0].innerHTML = len;
var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
inp1.id += len;
inp1.value = '';
var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
inp2.id += len;
inp2.value = '';
x.appendChild( new_row );
}
</script>
<!--<style type="text/css" title="currentStyle">
#import "tran/css/layout-styles.css";
#import "tran/css/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>-->
<script type="text/javascript" src="tran/js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="tran/js/jquery-ui-1.8.14.custom.min.js"></script>
<script type="text/javascript" src="tran/js/jq-ac-script.js"></script>
</head>
<body id="main_body" >
<img id="top" src="form2/top.png" alt="">
<div id="form_container">
<h1><a>Untitled Form</a></h1>
<form id="makhtab" class="appnitro" enctype="multipart/form-data" method="post" action="">
<div class="form_description">
<h2>Makhtab Details</h2>
<!--<p>This is your form description. Click here to edit.</p>-->
</div>
<table border="0px" width="100%">
<tr>
<td><label class="description" for="element_1">Code</label></td><td><input id="element_1" name="code" class="element text small" type="text" maxlength="6" value=""/></td>
</tr>
<tr>
<td><label class="description" for="element_1">Name</label></td><td><input id="element_1" name="name" class="element text large" type="text" maxlength="40" value=""/> </td>
</tr>
<tr>
<td><label class="description" for="element_1">Address</label></td><td><input id="element_4_1" name="add1" class="element text large" value="" type="text"></td>
</tr>
<tr>
<td></td><td><input id="element_4_1" name="add2" class="element text large" value="" type="text"></td>
</tr>
<tr>
<td></td><td><input id="element_4_1" name="add3" class="element text large" value="" type="text"></td>
</tr>
<tr>
<td><label class="description" for="element_1">City</label></td><td><select name="cmbcity" class="element text medium" style="font-size:18px;"/>
<option value=""></option>
<?php
mysql_connect("localhost", "root", '') or die(mysql_error());
mysql_select_db("webapp") or die(mysql_error());
$result = mysql_query("SELECT `name` from city ") or die(mysql_error());
while ($year = mysql_fetch_array($result)) {
echo "<option value='$year[name]'>$year[name]</option>";
}
?>
</select>
</tr>
<tr>
<td><label class="description" for="element_1">State</label></td><td><select name="cmbstate" class="element text medium" style="font-size:18px;"/>
<option value=""></option>
<?php
mysql_connect("localhost", "root", '') or die(mysql_error());
mysql_select_db("webapp") or die(mysql_error());
$result = mysql_query("SELECT `name` from state ") or die(mysql_error());
while ($year = mysql_fetch_array($result)) {
echo "<option value='$year[name]'>$year[name]</option>";
}
?>
</select>
</tr>
</tr>
<tr>
<td><label class="description" for="element_1">Country</label></td><td><select name="cmbcountry" class="element text medium" style="font-size:18px;"/>
<option value=""></option>
<?php
mysql_connect("localhost", "root", '') or die(mysql_error());
mysql_select_db("webapp") or die(mysql_error());
$result = mysql_query("SELECT `name` from country ") or die(mysql_error());
while ($year = mysql_fetch_array($result)) {
echo "<option value='$year[name]'>$year[name]</option>";
}
?>
</select>
<tr>
<td><label class="description" for="element_1">Telephone</label></td><td><input id="element_1" name="country" class="element text medium" type="text" maxlength="255" value=""/></td>
</tr>
<tr>
<td><label class="description" for="element_1">Mobile</label></td><td><input id="element_1" name="country" class="element text medium" type="text" maxlength="10" value=""/></td>
</tr>
<tr>
<br />
</tr>
</table>
<tr>
<br /><p style="border-bottom: 1px dotted #ccc;"></p><br />
<div class="form_description">
<h2>Zimmedar Details</h2>
<!--<p>This is your form description. Click here to edit.</p>-->
</div>
</tr>
<input type="button" id="addmorezimbutton" value="Add zimmedar" onclick="insRow()"/><br/><br/>
<table id="zimtable" border="0px" size="100px" cellspacing="0" cellpadding="2">
<tr>
<td width="10" align="center"><label class="description">Srno. </label></td>
<td width="118"><label class="description">Zimmedar Name</label></td>
<td width="118"><label class="description">Mobile</label></td>
<td width="10"></td>
<td width="10" align="center"></td>
</tr>
<tr>
<td>1</td>
<td width="210"><input size="40" maxlenght="40" type="text" id="zimame[]"/></td>
<td width="80"><input size="10" maxlenght="10" type="text" id="zimmob[]"/></td>
<!--<td><input size="45" maxlenght="10" type="text" id="momob"/> </td>-->
<td><input type="button" id="addzimmore" value="+" onclick="inszimRow()"/></td>
<td align="center"><input type="button" id="delzimbutton" value="-" onclick="deletezimRow(this)"/></td>
</tr>
</table>
<tr>
<br /><p style="border-bottom: 1px dotted #ccc;"></p><br />
<div class="form_description">
<h2>Muallim Details</h2>
<!--<p>This is your form description. Click here to edit.</p>-->
</div>
</tr>
<input type="button" id="addmorePOIbutton" value="Add Muallims" onclick="insRow()"/><br/><br/>
<table id="POITable" border="0px" size="100px" cellspacing="0" cellpadding="2">
<tr>
<td width="10" align="center"><label class="description">Srno. </label></td>
<td width="118"><label class="description">Moallim Name</label></td>
<td width="118"><label class="description">Mobile</label></td>
<td width="118"><label class="description">Qabiliyat</label></td>
<td width="10"></td>
<td width="10" align="center"></td>
</tr>
<tr>
<td>1</td>
<td width="210"><input size="40" maxlenght="40" type="text" id="moname[]"/></td>
<td width="80"><input size="10" maxlenght="10" type="text" id="momob[]"/></td>
<td><select name="cmbmuallim[]" class="element text large" style="font-size:18px;"/>
<option value=""></option>
<?php
mysql_connect("localhost", "root", '') or die(mysql_error());
mysql_select_db("webapp") or die(mysql_error());
$result = mysql_query("SELECT `name` from qabiliyat ") or die(mysql_error());
while ($year = mysql_fetch_array($result)) {
echo "<option value='$year[name]'>$year[name]</option>";
}
?>
</select></td>
<!--<td><input size="45" maxlenght="10" type="text" id="momob"/> </td>-->
<td><input type="button" id="addmore" value="+" onclick="insRow()"/></td>
<td align="center"><input type="button" id="delPOIbutton" value="-" onclick="deleteRow(this)"/></td>
</tr>
</table>
<br /><p style="border-bottom: 1px dotted #ccc;"></p><br />
<table border="0px" width="85%">
<tbody><tr>
<td width="105"><label class="description">No. of Students</label></td>
<td width="65"><input type="text" name=stu" size="5" maxlength="5"></input></td>
<td width="105"><label class="description">No. of Batches</label></td><td width="14"><input type="text" name="batch" size="5" maxlength="3"></input></td>
</tr>
<tr>
</tr>
</tbody>
</table>
</div>
</div>
</form>
</table>
</form>
</body>
</html>
Correct me if am wrong.
I didnt see any code to open a mysql database connection.
See this example.
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
} else {
echo "Error creating database: " . mysql_error();
}
?>
Before inserting records using mysql_query, make sure that you opened a mysql db connection like above.

Date picker & combo box values are not going to mysql

I am making a registration form in PHP and when submitting the form Date(Check In, for which I have used Date Picker), Combo boxes(Sex & Room No) & check box(taxi) is not getting inserted.
I have looked enough in to the code but not getting what is wrong..though other combo box and check box working just fine.
PHP Code -
<?php
$passport = $_POST['passport'];
$name = $_POST['name'];
$sex = $_POST['sex'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$country = $_POST['country'];
$contact = $_POST['contact'];
$email = $_POST['email'];
$roomNo = $_POST['roomNo'];
if(isset($_POST['food']) && $_POST['food'] == 'food')
{
$food = 'yes';
}
else
{
$food = 'no';
}
if(isset($_POST['car']) && $_POST['car'] == 'car')
{
$car = 'yes';
}
else
{
$car = 'no';
}
if(isset($_POST['others']) && $_POST['others'] == 'others')
{
$others = 'yes';
}
else
{
$others = 'no';
}
$checkIn = $_POST['checkIn'];
mysql_connect("localhost","root","");
mysql_select_db("guesthouse");
$personal_query = "INSERT INTO personal_details VALUES(
'',
'$name',
'$sex',
'$address1',
'$address2',
'$city',
'$country',
'$contact',
'$email')";
mysql_query($personal_query);
$result = mysql_affected_rows();
if($result == 1)
{
echo "Personal Details Submitted";
}
$booking_query = "INSERT INTO booking VALUES(
'',
'$name',
'$roomno',
'$food',
'$taxi',
'$others',
'$checkIn')";
mysql_query($booking_query);
$result = mysql_affected_rows();
if($result == 1) {
echo "<br/>Booking Details Submitted";
}
?>
HTML -
<!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" />
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
<script type="text/javascript" src="jsDatePick.min.1.3.js"></script>
<script type="text/javascript">
window.onload = function(){
new JsDatePick({
useMode:2,
target:"checkIn",
dateFormat:"%d-%M-%Y"
/*selectedDate:{
day:5,
month:9,
year:2006
},
yearsRange:[1978,2020],
limitToToday:false,
cellColorScheme:"beige",
dateFormat:"%m-%d-%Y",
imgPath:"img/",
weekStartDay:1*/
});
};
</script>
</head>
<form name="form1" method="post" action="registration_handle.php">
<table width="60%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" align="center" bgcolor="#0099FF">Room Reservation Details :</td>
</tr>
<tr>
<td width="33%" align="center" bgcolor="#66FFCC">Passport No </td>
<td width="67%"><label for="textfield"></label>
<input type="text" name="passport" id="passport"></td>
</tr>
<tr>
<td align="center" bgcolor="#66FFCC">Name</td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td align="center" bgcolor="#66FFCC">Sex</td>
<td><label for="select3"></label>
<select name="sex" id="sex">
<option value="" selected>Male</option>
<option value="" >Female</option>
</select></td>
</tr>
<tr>
<td align="center" bgcolor="#66FFCC">Address 1</td>
<td><input type="text" name="address1" id="address1"></td>
</tr>
<tr>
<td align="center" bgcolor="#66FFCC">Address2</td>
<td><input type="text" name="address2" id="address2"></td>
</tr>
<tr>
<td align="center" bgcolor="#66FFCC">City</td>
<td><input type="text" name="city" id="city" /></td>
</tr>
<tr>
<td align="center" bgcolor="#66FFCC">Country</td>
<td><label for="select2"></label>
<select name="country" id="country">
<?php
mysql_connect("localhost","root","");
mysql_select_db("guesthouse");
$query = "SELECT name FROM country";
$query_result = mysql_query($query);
while($result = mysql_fetch_assoc($query_result))
{
?>
<option value = "<?php echo $result['name'] ?>"><?php echo $result['name'] ?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td align="center" bgcolor="#66FFCC">Contact No</td>
<td><input type="text" name="contact" id="contact"></td>
</tr>
<tr>
<td align="center" bgcolor="#66FFCC">E-Mail </td>
<td><input type="text" name="email" id="email"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td align="center" bgcolor="#66FFCC">Room No</td>
<td><label for="select4"></label>
<select name="roomNo" id="roomNo">
<?php
mysql_connect("localhost","root","");
mysql_select_db("guesthouse");
$query = "SELECT name FROM roomno";
$query_result = mysql_query($query);
while($result = mysql_fetch_assoc($query_result))
{
?>
<option value = "<?php echo $result['name'] ?>"><?php echo $result['name'] ?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td align="center" bgcolor="#66FFCC">Extra Service</td>
<td><p>
<label> </label>
<input type="checkbox" name="food" value="food" />Food
<input type="checkbox" name="car" value="car" />Car
<input type="checkbox" name="others" value="others" />Others<br>
</p></td>
</tr>
<tr>
<td align="center" bgcolor="#66FFCC">Check In </td>
<td><label for="textfield3"></label>
<input type="text" size="16" name="checkIn" id="checkIn"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value="Submit">
<input type="reset" name="Reset" id="Reset" value="Reset" />
<input type="submit" name="cancel" id="cancel" value="Cancel" /></td>
</tr>
</table>
</form>
</html>
value of sex is "" (if you want default, don't type value="")
-Tested-
<html>
<body>
<select onchange="alert(this.value);">
<option>Volvo</option>
<option>Saab</option>
<option value=''>Mercedes</option>
<option>Audi</option>
</select>
</body>
</html>
you never give any value to $taxi (wich is called car in your form)
You initialte $roomNo and store $roomno
--- found these errors because I tend to do same things. here's a tip, always verify your variables name first, 99% of your probs will be fixed --- (works for me :) )

How to place my message where I want it using php

this is the code I have, I need my message to appear in the top of my form but I got an error.
I need to print my message right where I put the code print $msg
any idea about this please.
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table width="300" border="0">
<tr>
<td colspan="2"><?php print($msg); ?></td>
</tr>
<tr>
<td> *First name</td>
<td><span id="sprytextfield3">
<input type="text" value="" name="RFname" />
<br>
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*Last name</td>
<td><span id="sprytextfield4">
<input type="text" value="" name="RLname" />
<br>
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*User name</td>
<td><span id="sprytextfield1">
<input type="text" name="UserName" value="" />
<br />
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*Password</td>
<td><span id="sprypassword1">
<input type="password" name="UserPass" value="" />
<span class="passwordRequiredMsg"><br />
A value is required.</span></span></td>
</tr>
<tr>
<td>*Email</td>
<td><span id="sprytextfield2">
<input type="text" name="UserEmail" value="" />
<br />
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
</tr>
<tr>
<td>Web site</td>
<td><input type="text" name="WebSite" value="" /></td>
</tr>
<tr>
<td> </td>
<td><input name="submited" type="submit" onClick="MM_swapImage('Image1','','images/blog.png',1)" value="Register"/></td>
</tr>
</table>
</form>
</div>
<div class="image_NewUserRegister"><iframe src="images/Untitled-2.jpg" name="now_regist" width="207" height="280" scrolling="no" frameborder="0"></iframe></div>
</div>
<?php
if(isset($_POST['submited'])) {
$RFname = $_POST['RFname'];
$RLname = $_POST['RLname'];
$USERname = $_POST['UserName'];
$USERpassword = $_POST['UserPass'];
$USERemail = $_POST['UserEmail'];
$USERwebsite = $_POST['WebSite'];
$check=$db->query("select 1 from loginaccess where Email ='".$USERemail."'");
if(mysqli_num_rows($check) > 0 ) {
$msg = "<span style='color:#F00; font-size:14px; font-weight:bold;'>This email already taken</span>";
}
this is the code I have, I need my message to appear in the top of my form but I got an error. I need to print my message right where I put the code print $msg
any idea about this please.
You really need to set the message before you try to output it.
To elaborate on David's answer: move the PHP block before the form markup, like so:
<?php
if(isset($_POST['submited'])) {
$RFname = $_POST['RFname'];
$RLname = $_POST['RLname'];
$USERname = $_POST['UserName'];
$USERpassword = $_POST['UserPass'];
$USERemail = $_POST['UserEmail'];
$USERwebsite = $_POST['WebSite'];
$check=$db->query("select 1 from loginaccess where Email ='".$USERemail."'");
if(mysqli_num_rows($check) > 0 ) {
$msg = "<span style='color:#F00; font-size:14px; font-weight:bold;'>This email already taken</span>";
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table width="300" border="0">
<tr>
<td colspan="2"><?php print($msg); ?></td>
</tr>
<tr>
<td> *First name</td>
<td><span id="sprytextfield3">
<input type="text" value="" name="RFname" />
<br>
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*Last name</td>
<td><span id="sprytextfield4">
<input type="text" value="" name="RLname" />
<br>
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*User name</td>
<td><span id="sprytextfield1">
<input type="text" name="UserName" value="" />
<br />
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td>*Password</td>
<td><span id="sprypassword1">
<input type="password" name="UserPass" value="" />
<span class="passwordRequiredMsg"><br />
A value is required.</span></span></td>
</tr>
<tr>
<td>*Email</td>
<td><span id="sprytextfield2">
<input type="text" name="UserEmail" value="" />
<br />
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
</tr>
<tr>
<td>Web site</td>
<td><input type="text" name="WebSite" value="" /></td>
</tr>
<tr>
<td> </td>
<td><input name="submited" type="submit" onClick="MM_swapImage('Image1','','images/blog.png',1)" value="Register"/></td>
</tr>
</table>
</form>
</div>
<div class="image_NewUserRegister"><iframe src="images/Untitled-2.jpg" name="now_regist" width="207" height="280" scrolling="no" frameborder="0"></iframe></div>
</div>
HTH.

Categories