i have trouble inserting my with php into one table of mysql, i can insert in any other table but this one here is my code (is a test code)
This is the formulario.php code
<form action="opformulario.php" method="POST">
<br><br>
<input type="text" name="nombre" placeholder="Nombre" />
<br><br>
<input type="text" name="apellido" placeholder="apellido" />
<br><br>
<input type="text" name="correo" placeholder="correo" />
<br><br>
<input type="text" name="nombre2" placeholder="Nombre" />
<br><br>
<input type="text" name="apellido2" placeholder="apellido" />
<br><br>
<input type="date" name="apellido3" placeholder="apellido" />
<input type="submit" value="aceptar" />
and this is the op formulario code
include("conexion.php");
$nombre = $_POST['nombre'];
$apellido = $_POST['apellido'];
$correo = $_POST['correo'];
$nombre2 = $_POST['nombre2'];
$apellido2 = $_POST['apellido2'];
$apellido3 = $_POST['apellido3'];
$FechaMySQL = implode( '-', array_reverse( explode( '/', $apellido3 ) ) ) ;
$query = " INSERT INTO cliente (nom_raz,nom_com,tel_cli,cel_cli,email_cli,fecha_reg) VALUES('$nombre','$apellido','$correo','$nombre2','$apellido2','$FechaMySQL')";
$resultado = $conexion->query($query);
if ($resultado){
echo "Succesfull";
}
else{
echo "Error:<br />".mysql_error();
}
this is the table in which i am trying to insert
Mysql 'cliente' table
Related
I am using postgreSQL, PHP, and an HTML form. The form is a simple scholarship application. My connection script seems to work fine because when I click submit on the HTML form it echos "connected" and doesn't die, however no data from the form is transferred to my table. Please any guidance.
My PHP connection script: connect.php
try {
$dbConn = new PDO('pgsql:host=' . DB_HOST . ';'
. 'port=' . DB_PORT . ';'
. 'dbname=' . DB_NAME . ';'
. 'user=' . DB_USER . ';'
. 'password=' . DB_PASS);
$dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Set error mode to exception
echo “Connected”;
} catch (PDOException $e) {
$fileName = basename($e->getFile(), ".php"); // File that triggers the exception
$lineNumber = $e->getLine(); // Line number that triggers the exception
die("[$fileName][$lineNumber] Database connect failed: " . $e->getMessage() . '<br/>');
}
?>
My PHP submission script: form.php
<?php
require 'connect.php';
$sid = $_POST['sid'];
$firstName = $_POST['fname'];
$preferredName = $_POST['pname'];
$lastName = $_POST['lname'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$inSchool = $_POST['inSchool'];
$gDate = $_POST['gDate'];
$gpa = $_POST['gpa'];
$essay = $_POST['essay'];
$submit = $_POST['submit'];
if ($sid = ''){
$query = 'insert into student(firstname,lastname,prefname,address,city,state,zip,phone,email) values (?,?,?,?,?,?,?,?,?)';
$statement = $dbConn->prepare($query);
$statement->execute([$firstname, $lastname,$preferredname,$address,$city,$state,$zip,$phone,$email]);
}
else{
$query = 'insert into student(firstname,lastname,prefname,address,city,state,zip,phone,email) values (?,?,?,?,?,?,?,?,?)';
$statement = $dbConn->prepare($query);
$statement->execute([$firstname, $lastname,$preferredname,$address,$city,$state,$zip,$phone,$email]);
}
$query = 'select sid from student where firstname = ? and lastname = ? limit 1';
$statement = $dbConn->prepare($query);
$statement->execute([$firstname, $lastname,$preferredname,$address,$city,$state,$zip,$phone,$email]);
$results = $statement->fetch();
echo $results[0];
?>
My HTML Form: Application.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="Style.css">
<title>Application</title>
</head>
<body>
<form action="form.php" method="post">
<header>
<img class="logo" src="" alt="logo">
<div>Asterisks Corporation Scholarship Application</div>
</header>
<p class="general">Please fill in all information to apply for the Asterisk's Scholarship.<p>
First Name:
<input type="text" size="15" name="fname" required />
Preferred Name:
<input type="text" size="15" name="pname" >
Last Name:
<input type="text" size="15" name="lname" required />
<p class="newSection">Contact Information</p>
Address:
<input type="text" size="50" name="address" required />
City:
<input type="text" size="15" name="city" required />
State:
<input type="text" size="15" name="state" required />
Zip Code:
<input type="text" size="1" name="zip" placeholder="####" required /><br><br>
Phone Number:
<input type="text" size="10" name="phone" placeholder="(###)-###-###">
Email Address:
<input type="text" size="50" name="email" required />
<p class="newSection">Academic Information</p>
Are you currently enrolled in school?
<input type="checkbox" id="Yes" name="yesBox">
<label for="Yes">Yes</label>
<input type="checkbox" id="No" name="noBox">
<label for="No">No</label><br><br>
What school are you enrolled?
<input type="text" size="50" name="schoolsEnrolled"><br><br>
What is/was your date of Graduation?
<input type="date" name="graduationDate"><br><br>
GPA
<input type="text" size="1" name="gpa">
<p class="newSection">What institutions have you applied?</p>
<input type="text" name="" value="" id="school" name="schoolsApplied">
<button onclick="addToList()" type="button" name="button" id="addButton">Add School</button><br>
<ul id="schoolList"></ul>
<p class="newSection">Please write a small essay as to why you should receive this scholarship and what your plans are after graduation.</p>
<textarea id="essay" style="width: 500px; height: 200px;" alignment="left" onkeyup="wordCounter(),wordsRemaining()" name="essay"></textarea>
<div>
300 Words Minimum & 500 Words Maximum
<div> Word Count: <span id="wordCount">0</span></div>
<div> Words Remaining: <span id="wordsRemaining">0</span></div>
</div>
<p class="newSection">Please confirm each of the following:</p>
<input type="checkbox" id="Transcript" name="transcriptConfirm" required />
<label for="Transcript">I have sent in all of my transcripts</label><br>
<input type="checkbox" id="Schools" name="schoolConfirm" required />
<label for="Schools">All schools that I am considering are in the US</label><br>
<input type="checkbox" id="Awards" name="awardConfirm" required />
<label for="Awards">I understand that the award is $5,000 per year for four years</label><br>
<input type="checkbox" id="Confirm" name="amountConfirm" required />
<label for="Confirm">I have received confirmation that my recommenders have emailed their letters to the Scholarship's Coordinator</label><br><br>
Please type your signature in the text box below: <br><br>
<input type="text" size="20" name="signature" required />
<div><br>
<input type="submit" value ="Submit" name="submit">
<input type="reset" value="Start Over" onclick="MinMax()">
</div>
<script>
function addToList(){
let school= document.getElementById("school").value;
document.getElementById("schoolList").innerHTML += ('<li>'+ school+'</li>');
};
function wordCounter(text){
var count= document.getElementById("wordCount");
var input= document.getElementById("essay");
var text=essay.value.split(' ');
var wordCount = text.length;
count.innerText=wordCount
}
function wordsRemaining(text){
var count= document.getElementById("wordCount");
var input= document.getElementById("essay");
var remaining = document.getElementById("wordsRemaining");
var text=essay.value.split(' ');
var wordCount = text.length;
remaining.innerText=300-wordCount
}
</script>
</form>
</body>
</html>
I am trying to fill a html form with data being received out of my mysql database. However I cannot set the forms to display on-load the variables being extracted from the database. I would like the form on-load to hold the data last entered into the forms which have been added to the database previously.
$query = "SELECT FROM character_tbl WHERE character_player
='".$_SESSION["user"]."' character_tbl";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$name = $row['character_name'];
$race = $row['character_race'];
$class = $row['character_class'];
$alignment = $row['character_alignment'];
$hp = $row['character_hp'];
$str = $row['character_str'];
$dex = $row['character_dex'];
$con = $row['character_con'];
$int = $row['character_int'];
$wis = $row['character_wis'];
$cha = $row['character_cha'];
$ac = $row['character_ac'];
$touch = $row['character_touch'];
$flat = $row['character_flat'];
$fort = $row['character_fort'];
$ref = $row['character_ref'];
$will = $row['character_will'];
}
echo $will;
mysql_close();
?>
<!DOCTYPE html>
<html>
<body>
<div id="nav">
<form action="user.php">
<input type="submit" value="Back">
</form>
</div>
<div id="section">
<form action="update.php" method="POST">
Character Name:<br>
<input type="text" name="name" value="<?php echo $name;?>">
<br>
Race<br>
<input type="text" name="race" value="<?php echo $race;?>">
<br>
Class<br>
<input type="text" name="class" value="<?php echo $class;?>">
<br>
Alignment<br>
<input type="text" name="alignment" value="<?php echo $alignment;?>">
<br>
HP<br>
<input type="text" name="hp" value="<?php echo $hp;?>">
<br>
STR<br>
<input type="number" name="str" value="<?php echo $str;?>">
<br>
DEX<br>
<input type="number" name="dex" value="<?php echo $dex;?>">
<br>
CON<br>
<input type="text" name="con" value="<?php echo $con;?>">
<br>
INT<br>
<input type="text" name="int" value="<?php echo $int;?>">
<br>
WIS<br>
<input type="text" name="wis" value="<?php echo $wis;?>">
<br>
CHA<br>
<input type="text" name="cha" value="<?php echo $cha;?>">
<br>
AC<br>
<input type="text" name="ac" value="<?php echo $ac;?>">
<br>
Touch AC<br>
<input type="text" name="touch" value="<?php echo $touch;?>">
<br>
Flat-Footed AC<br>
<input type="text" name="flat" value="<?php echo $flat;?>">
<br>
Fortitude<br>
<input type="text" name="fort" value="<?php echo $fort;?>">
<br>
Reflex<br>
<input type="text" name="ref" value="<?php echo $ref;?>">
<br>
Will<br>
<input type="text" name="will" value="<?php echo $will;?>">
</br>
<input type="submit" value="Update">
</form>
I think the SQL has error:
SELECT FROM character_tbl WHERE character_player
try:
SELECT * FROM character_tbl WHERE character_player
You have syntax error in your mysql query. You have not place field or columns name or (*) for all columns to extract.
Try like this..
$query = "SELECT * FROM character_tbl WHERE character_player ='".$_SESSION['user']."'";
I'm trying to fill my mysqli database from a form with multiple input fields with the same structure and names using ajax, but I'm pretty stuck here. I have tried several solutions but all with te same result, only my last two input fields are send to my database.
My form looks like this;
<form id="sendform" method="post" action="#">
<fieldset>
<input type="text" value="JD01" id="shortname" name="member[shortname]" readonly />
<input type="text" value="John Doe" id="fullname" name="member[fullname]" readonly />
</fieldset>
<fieldset>
<input type="text" value="JD02" id="shortname" name="member[shortname]" readonly />
<input type="text" value="Jane Doe" id="fullname" name="member[fullname]" readonly />
</fieldset>
<input type="submit" value="Send" />
</form>
The ajax part;
$('#sendform').submit(function(e){
$.ajax({
type: 'POST',
url: 'inc/post.php',
data: $(this).serialize(),
success: function(data){
alert("Data Save: " + data);
}
});
e.preventDefault();
});
And my php;
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'root');
define('DBNAME', 'test');
$db = new mysqli(HOST, USER, PASS, DBNAME);
$values = array();
foreach ($_POST['member'] as $member) {
$values[] = '(' . $member['shortname'] . ',' . $member['fullname'] . ')';
}
$sql = "INSERT INTO sendform (shortname, fullname) VALUES " . implode(',', $values);
$result = $db->query($sql);
if($result) {
echo "Yep";
}
$db->close();
What am I doing wrong here? thnx!
Try this:
<form id="sendform" method="post" action="#">
<fieldset>
<input type="text" value="JD01" id="shortname" name="member[shortname][]" readonly />
<input type="text" value="John Doe" id="fullname" name="member[fullname][]" readonly />
</fieldset>
<fieldset>
<input type="text" value="JD02" id="shortname" name="member[shortname][]" readonly />
<input type="text" value="Jane Doe" id="fullname" name="member[fullname][]" readonly />
</fieldset>
<input type="submit" value="Send" />
</form>
And PHP Page:
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'root');
define('DBNAME', 'test');
$db = new mysqli(HOST, USER, PASS, DBNAME);
$values = array();
for($i=0 ;$i < count($_POST['member']); $i++) {
$values[] = '("' . $_POST['member']['shortname'][$i] . '","' . $_POST['member']['fullname'][$i] . '")';
}
$sql = "INSERT INTO sendform (shortname, fullname) VALUES " . implode(',', $values);
$result = $db->query($sql);
if($result) {
echo "Yep";
}
$db->close();
You can make the form look like this (note that member[] is now an array itself)
<form id="sendform" method="post" action="">
<fieldset>
<input type="text" value="JD01" id="shortname" name="member[1][shortname]" readonly />
<input type="text" value="John Doe" id="fullname" name="member[1][fullname]" readonly />
</fieldset>
<fieldset>
<input type="text" value="JD02" id="shortname" name="member[2][shortname]" readonly />
<input type="text" value="Jane Doe" id="fullname" name="member[2][fullname]" readonly />
</fieldset>
<input type="submit" value="Send" />
</form>
in your PHP use
foreach ($_POST['member'] as $member) {
$values[] = '(' . $member['shortname'] . ',' . $member['fullname'] . ')';
}
You are using member[shortname] and member[fullname] two times in html. So values from second fieldset are overriding the first one.
Hence we are getting only the values the are put in second fieldset.
You can try using following code:
<form id="sendform" method="post" action="#">
<fieldset>
<input type="text" value="JD01" id="shortname" name="member['shortname'][0]" readonly />
<input type="text" value="John Doe" id="fullname" name="member['fullname'][0]" readonly />
</fieldset>
<fieldset>
<input type="text" value="JD02" id="shortname" name="member['shortname'][1]" readonly />
<input type="text" value="Jane Doe" id="fullname" name="member['fullname'][1]" readonly />
</fieldset>
<input type="submit" value="Send" />
</form>
And my php, make the following changes:
$values = array();
$i = 0;
foreach ($_POST['member'] as $member) {
$values[] = '(' . $member['shortname'][$i] . ',' . $member['fullname'][$i] . ')';
$i++;
}
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[date][]" required placeholder="Enter Date (yy-mm-dd)"></td>
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[msg][]" required placeholder="Enter Expenses Here"></td>
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[vendor][]" required placeholder="Vendor Name Here"></td>
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[amount][]" required placeholder="Enter Amount Here"></td>
<td class="text-left"><input type="text" class="form-control input-lg" name="expense[prove][]" required placeholder="Prove"></td>
PHP Code
<?php
if(isset($_POST['submit'])){
//$date = mysql_real_escape_string($_POST['date']);
//$date = strtotime($date);
// $date = date('Y-m-d', $date);
//$msg = mysql_real_escape_string($_POST['msg']);
//$vendor = mysql_real_escape_string($_POST['vendor']);
//$amount = mysql_real_escape_string($_POST['amount']);
//$prove = mysql_real_escape_string($_POST['prove']);
$values = array();
for($i=0 ;$i < count($_POST['expense']); $i++) {
$values[] = '("' . $_POST['expense']['date'][$i] . '","' . $_POST['expense']['msg'][$i] . '","' . $_POST['expense']['vendor'][$i] . '","' . $_POST['expense']['amount'][$i] . '","' . $_POST['expense']['prove'][$i] . '")';
}
$sql = ("INSERT INTO expenses (date, msg, vendor, amount, prove) VALUES " . implode(',', $values));
echo "<script>alert('Your expenses data inserted successfully!')</script>";
echo "<script>window.open('expenses.php','_self')</script>";
exit();
}
?>
show me the error Notice: Undefiened offset:2 in ..............
So I have a form that users fill out and it adds to a database. Everything works except for the date. The date shows up as 0000-00-00. Currently, I'm using a datepicker, but the date wasn't showing up even when the user had to type it in...so that shouldn't be the issue.
<form name="Add" id="Add" method="post" action="programadd.php">
<p>Program Name:
<input name="program" type="text" id="program" />
</p>
<p>Air Date
<input name="airdate" type="text" id="airdate" />
</p>
<p>Description
<input name="description" type="text" id="description" s />
</p>
<p>Production
<input name="production" type="text" id="production" />
</p>
<p>Promotions
<input name="promotion" type="text" id="promotion" />
</p>
<p>Community
<input name="community" type="text" id="community" />
</p>
<p>Web
<input name="web" type="text" id="web" />
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
And here's the code that adds it to the database:
<?php require_once('connect-db.php');
$program = $_POST['program'];
$airdate = $_POST['airdate'];
$description = $_POST['description'];
$production = $_POST['production'];
$promotion = $_POST['promotion'];
$community = $_POST['community'];
$web = $_POST['web'];
if (mysql_query ("INSERT INTO calendar(program, airdate, description, production, community, promotion, web) VALUES
('$program', '$airdate', '$description','$production', '$promotion', '$community', '$web')"))
{ echo "Program successfully added to the database <br />";
}
else {
die(mysql_error());
}
require_once("db_connx_close.php");
?>
One other question, how do I get the date to display as Month/Date/Year, instead of the reverse?
$date = date('Y-m-d', strtotime($_POST['airdate']));
I want to make a text field and button that will allow the user to fetch his details on the text fields instead of writting his details every time he wants to make a new reservation.
like in this picture:
http://oi41.tinypic.com/23ie70j.jpg
I tried to make this but with my code but gives me double forms one with the details and one without.
<form method="post" action="reserv page.php">
enter the email: <input type = "text" name = "email"/>
<input type = "submit" name = "submit" value="submit" />
</form>
<?php
mysql_connect("localhost","userName","password");
mysql_select_db("database_Name");
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$q = "SELECT * FROM tabe WHERE the_email = '$email'";
$run = mysql_query($q );
while($row = mysql_fetch_array($run))
{
?>
</br></br>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" value="<?php echo $row[1]; ?>" />
Last Name: <input name="lNamet" type="text" value="<?php echo $row[2]; ?>" />
User Name: <input name="uName" type="text" value="<?php echo $row[3]; ?>"/>
Email: <input name="email" type="text" value="<?php echo $row[4]; ?>" />
password: <input name="pass" type="password" value="<?php echo $row[5]; ?>"/>
contact: <input name="number" type="text" value="<?php echo $row[6]; ?>" />
<input name="confirm" type="submit" value="Confirm" />
</form>
</br></br>
<?php
}}
?>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" />
Last Name: <input name="lNamet" type="text" />
User Name: <input name="uName" type="text" />
Email: <input name="email" type="text" />
password: <input name="pass" type="password" />
contact: <input name="number" type="text" />
<input name="confirm" type="submit" value="Confirm" />
</form>
<form method="post" action="reserv page.php">
enter the email: <input type = "text" name = "email"/>
<input type = "submit" name = "submit" value="submit" />
</form>
<?php
mysql_connect("localhost","userName","password");
mysql_select_db("database_Name");
if(isset($_POST['submit']))
{
$email = $_POST['email'];
//limit the query to one entry :)
$q = "SELECT * FROM tabe WHERE the_email = '$email' LIMIT 1";
$run = mysql_query($q );
//check if email is registered
if(mysql_num_rows($run)>0)
{
//display filled up form
while($row = mysql_fetch_array($run))
{
?>
</br></br>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" value="<?php echo $row[1]; ?>" />
Last Name: <input name="lNamet" type="text" value="<?php echo $row[2]; ?>" />
User Name: <input name="uName" type="text" value="<?php echo $row[3]; ?>"/>
Email: <input name="email" type="text" value="<?php echo $row[4]; ?>" />
password: <input name="pass" type="password" value="<?php echo $row[5]; ?>"/>
contact: <input name="number" type="text" value="<?php echo $row[6]; ?>" />
<input name="confirm" type="submit" value="Confirm" />
</form>
</br></br>
<?php
}
}
//display blank form
else{
?>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" />
Last Name: <input name="lNamet" type="text" />
User Name: <input name="uName" type="text" />
Email: <input name="email" type="text" />
password: <input name="pass" type="password" />
contact: <input name="number" type="text" />
<input name="confirm" type="submit" value="Confirm" />
</form>
<?php
}
}
?>
You're getting 2 forms because you're echoing one form if $_POST['submit'] is set and then another one regardless of anything. Print the second form only if $_POST['submit'] is not set. Since your code is so poorly written I will just give you an example:
if(isset($_POST['submit'])){
PRINT FETCHED FORM
}else{
PRINT EMPTY FORM
}
This, however, is not the "right" way to go. What people actually do is have variables null'd at start and then fill them up with data if there's a request and have a single form written in the file with input values as those variables.