Php not inserting data - php

my code is not inserting any data on my php, im using a form that will display values but my code in update is not working. please help,
here is my code in php :
if (isset($_POST['update'])) {
$landowner_id = $_POST['landowner_id'];
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$municipality = $_POST['municipality'];
$barangay = $_POST['barnagay'];
$areacovered = $_POST['areacovered'];
$sex = $_POST['sex'];
mysqli_query($db, "UPDATE info SET firstname='$firstname', middlename='$middlename', lastnamename='$lastname', municipality='$municipality', barangay='$barangay', areacovered='$areacovered', sex='$sex' WHERE landowner_id=$landowner_id");
$_SESSION['message'] = "Address updated!";
}
here is my html
<div class="form-wrapper">
<input type="number" id = "check" name="firstname" placeholder="First Name" class="input-field" value="<?php echo $landowner_id;?>" required>
</div>
<div class="form-wrapper">
<input type="text" id = "check" name="firstname" placeholder="First Name" class="input-field" value="<?php echo $firstname;?>" required>
</div>
<div class="form-wrapper">
<input type="text" name="middlename" placeholder="Middle Name" class="input-field" value="<?php echo $middlename;?>">
</div>
<div class="form-wrapper">
<input type="text" name="lastname" placeholder="Last Name" class="input-field" value="<?php echo $lastname;?>" required>
</div>
<div class="form-wrapper">
<input type="text" name="municipality" placeholder="Municipality" class="input-field" value="<?php echo $municipality;?>" required>
</div>
<div class="form-wrapper">
<input type="text" name="barangay" placeholder="Barangay" class="input-field" value="<?php echo $barangay;?>" >
</div>
<div class="form-wrapper">
<input type="text" id = "check" name="areacovered" placeholder="Area Covered" class="input-field" value="<?php echo $areacovered;?>" required>
</div>
<div class="form-wrapper">
<input type="text" id = "check" name="sex" placeholder="Sex" class="input-field" value="<?php echo $sex;?>" required>
<br>
<button class="btn" type="submit" name="update" >Update</button>
</div>

I don't see any "form" tag. Are you missing to wrap your "form-wrapper" into a tag? Something like this:
<form action="" method="post">
<div class="form-wrapper">
<input type="number" id="check" name="firstname" placeholder="First Name" class="input-field" value="<?php echo $landowner_id; ?>" required>
</div>
<!-- Other inputs -->
<div class="form-wrapper">
<input type="text" id="check" name="sex" placeholder="Sex" class="input-field" value="<?php echo $sex; ?>" required>
<br>
<button class="btn" type="submit" name="update">Update</button>
</div>
</form>
Other important things to consider:
Never EVER send to the DB plain inputs coming from the outside without cleaning them! Otherwise, you will be open to SQL injection. Use prepare-statements to solve this issue.
Instead of mysqli_query I recommend you to use PDO. You can prepare statement super easy. Here you can see an example of usage: https://stackoverflow.com/a/60988740/3454593

Related

Saving contact form data in a file by pressing a submit button doesn't seem to work

I made a contact form with a submit button. I want all the information saved which got typed into the contact form. Somehow the submit button does nothing.
In the past it seemed to work but as a php/html newbie I just cant seem to find the problem
HTML
<form method="post">
<div class="contact-form" action="contact-form.php" method="post">
<div class="contentLeft">
<h4>Ihre Daten</h4>
<br>
<h3>Vorname</h3>
<input class="inputText" type="text" name="firstName" required="required">
<br>
<h3>Nachname</h3>
<input class="inputText" type="text" name="lastName" required="required">
<br>
<h3>E-Mail</h3>
<input class="inputText" type="text" name="mail" required="required">
<br>
<h3>Straße</h3>
<input class="inputText" type="text" name="street" required="required">
<br>
<h3>Postleitzahl</h3>
<input class="inputText" type="text" name="postal" required="required">
<br>
<h3>Stadt</h3>
<input class="inputText" type="text" name="city" required="required">
<br>
<h3>Telefonnummer</h3>
<input class="inputText" type="number" name="phone" required="required">
<br>
</div>
<div class="contentRight">
<h4>Ihre Nachricht</h4>
<br>
<h3>Betreff</h3>
<input class="inputTextRight" type="text" name="subject" required="required">
<br>
<h3>Buchungscode</h3>
<input class="inputTextRight" type="text" name="bookingCode" required="required">
<br>
<br><
<textarea></textarea>
</div>
<br>
<button class="button" type="submit" name="submit">Absenden</button>
</div>
</form>
PHP
if (isset($_POST['submit'])) {
$firstName = $_POST['firstname'];
$lastName = $_POST['lastname'];
$mail = $_POST['mail'];
$street = $_POST['street'];
$postal = $_POST['postal'];
$city = $_POST['city'];
$phone = $_POST['phone'];
$subject = $_POST["subject"];
$bookingCode = $_POST['bookingCode'];
$message = $_POST['message'];
$data=$_POST["firstName"] ."\n".$_POST["firstName"] ."\n".$_POST["lastName"] ."\n".$_POST["mail"] ."\n".$_POST["street"] ."\n". $_POST["postal"] ."\n".$_POST["city"] ."\n".$_POST["phone"] ."\n". $_POST["subject"] ."\n".$_POST["bookingCode"] ."\n". $_POST["message"];
$fp = fopen("data.txt", "a");
fwrite($fp, $data);
fclose($fp);
header ("Location: NewTest.html?mailsent");
}
?>
You added action of form in div, move that from div to form.
<form action="contact-form.php" method="post">
<div class="contact-form">
<div class="contentLeft">
<h4>Ihre Daten</h4>
<br>
<h3>Vorname</h3>
<input class="inputText" type="text" name="firstName" required="required">
<br>
<h3>Nachname</h3>
<input class="inputText" type="text" name="lastName" required="required">
<br>
<h3>E-Mail</h3>
<input class="inputText" type="text" name="mail" required="required">
<br>
<h3>Straße</h3>
<input class="inputText" type="text" name="street" required="required">
<br>
<h3>Postleitzahl</h3>
<input class="inputText" type="text" name="postal" required="required">
<br>
<h3>Stadt</h3>
<input class="inputText" type="text" name="city" required="required">
<br>
<h3>Telefonnummer</h3>
<input class="inputText" type="number" name="phone" required="required">
<br>
</div>
<div class="contentRight">
<h4>Ihre Nachricht</h4>
<br>
<h3>Betreff</h3>
<input class="inputTextRight" type="text" name="subject" required="required">
<br>
<h3>Buchungscode</h3>
<input class="inputTextRight" type="text" name="bookingCode" required="required">
<br>
<br><
<textarea></textarea>
</div>
<br>
<button class="button" type="submit" name="submit">Absenden</button>
</div>
</form>

I very new at both html and php. This is my first time posting. Add state code dropdown list

I have a standard form with all user info, I want to have a drop down list where the state input is so the user can select the state. I have a database with state code and description. Here's the code for my form, just need to know how to add the drop down list so it will show up where the state code is. I've tried putting the drop down using the regular drop down code but when I do that the drop down list shows up at the top of the form.
<section class="main-container">
<div class="main-warpper">
<h2>Sign up</h2>
<form class="signup-form" action="includes/signupinc.php" method="POST">
<input type="text" name="mbrnumber" placeholder="Member Number">
<input type="text" name="alias" placeholder="Alias">
<input type="text" name="firstname" placeholder="First Name">
<input type="text" name="lastname" placeholder="Last Name">
<input type="text" name="address" placeholder="Address">
<input type="text" name="city" placeholder="City">
<input type="text" name="state" placeholder="State">
<input type="text" name="zipcode" placeholder="Zip Code">
<input type="text" name="email" placeholder="E-mail">
<input type="password" name="pwd" placeholder="Password">
<button = type="submit" name="submit">Sign Up</button>
</form>
</div>
</section>
I've been playing around with this and have come up with something like I want. I would like the state code drop down list to look like the placeholder in the rest of the form. Width height I know I can style this but not sure just how to do that. Here's the code I have come up with. The state drop down list comes from my database.
<section class="main-container">
<div class="main-warpper">
<h2>Sign up</h2>
<form class="signup-form" action="includes/signup.inc.php" method="POST">
<input type="text" name="clubname" placeholder="Your databae name(lower case only no spaces)">
<input type="text" name="mbrnumber" placeholder="Member Number">
<input type="text" name="alias" placeholder="Alias">
<input type="text" name="firstname" placeholder="First Name">
<input type="text" name="lastname" placeholder="Last Name">
<input type="text" name="address" placeholder="Address">
<input type="text" name="city" placeholder="City">
<tr>
<input type="text" name="state" placeholder="State">
<select name="state">
<?php
$sql="Select * from matchstates";
echo "<option value='' selected>Select State</option>";
foreach ($conn->query($sql) as $row) {
echo "<option value=$row[statecode]>$row[statename]</option>";
}
?>
</select>
</tr>
<input type="text" name="zipcode" placeholder="Zip Code">
<input type="text" name="email" placeholder="E-mail">
<input type="password" name="pwd" placeholder="Password">
<button = type="submit" name="submit">Sign Up</button>
</form>
</div>

Trying to Insert data to multiple tables in single query in php

Im trying to add data to multiple tables using a single query.
These two tables are contacts and address. I think the issue is with my address table. IS it a good idea to separate my address table? since multiple contacts can share the same address.(family)
Query for inserting data
$sql = "INSERT INTO contacts (firstName,lastName,nickName,cellNumber,homeNumber,workNumber) VALUES ($firstName,$lastName,$nickName,$cellNumber,$homeNumber,$workNumber) "
. "INSERT INTO address(street,city,state,country) VALUES($street,$city,$state,$country) INSERT INTO contacts (email,birthday,memo)"
. "values($email,$birthday,$memo)";
My HTML form
<fieldset>
<legend>Register Form</legend>
<div>
<input type="text" name="firstName" placeholder="First Name"/>
</div>
<div>
<input type="text" name="lastName" placeholder="Last Name"/>
</div>
<div>
<input type="text" name="nickName" placeholder="Nick Name"/>
</div>
<div>
<input type="text" name="cellNumber" placeholder="Cell Number"/>
</div>
<div>
<input type="text" name="homeNumber" placeholder="Home Number"/>
</div>
<div>
<input type="text" name="workNumber" placeholder="Work Number"/>
</div>
<div>
<input type="text" name="street" placeholder="Street"/>
</div>
<div>
<input type="text" name="city" placeholder="City"/>
</div>
<div>
<input type="text" name="state" placeholder="state"/>
</div>
<div>
<input type="text" name="country" placeholder="country"/>
</div>
<div>
<input type="text" name="email" placeholder="Email"/>
</div>
<div>
<input type="text" name="birthday" placeholder="Birthday"/>
</div>
<div>
<div class="small"></div>
<textarea name="memo" placeholder="Memo"></textarea>
</div>
<input type="submit" name="addContact" value="Send"/>
</fieldset>
Thomas i try your query u should modify and try this query:
Code :
$sql = "INSERT INTO contacts (firstName,lastName,nickName,cellNumber,homeNumber,workNumber) VALUES ($firstName,$lastName,$nickName,$cellNumber,$homeNumber,$workNumber) " ; "INSERT INTO address(street,city,state,country) VALUES($street,$city,$state,$country)"; "INSERT INTO contacts (email,birthday,memo)
values($email,$birthday,$memo)";
I think it should be must helpful.

PHP variable into form ($username)

<form method="POST">
<h1>Enquiries</h1>
<span><h2 class="required">*Required</h2></span>
<div class="input-group">
<label class="sr-only">Firstname</label>
<input class="text" type="text" name="name" value="<?php print_r($username)?>" />
</div>
<label class="sr-only">Lastname</label>
<input class="text" type="text" name="Lastname" placeholder="*" required>
<label class="sr-only">Email</label>
<input class="text" type="text" name="email" placeholder="">
<label class="sr-only">Subject</label>
<input type="text" name="password" class="text" placeholder="*">
<label for="inputPassword" class="sr-only">Comment:</label>
<textarea id="text" placeholder="Write something..." required> </textarea>
<input type="submit" action="submit.php">
I need to input my $username into the firstname input field. How can I set the automatic value to be $username When a logged in user goes to send an enquiry.
<input class="text" type="text" name="name" value="<?php echo isset($username) ? $username : '' ?>" />
You need to use echo instead of print_r. Also check if the variable exists.
<input class="text" type="text" name="name" value="<?php echo !empty($username) ? $username : ''; ?>" />
Just try this
<input class="text" type="text" name="name" value="<?= $username ?>" />
Will fix it if the variable username has been initialized prior

Using the same script multiple times for one form (using PHP)

I am relatively new to PHP and programming, so please pardon my ignorance and language.
I have a form that has multiple instances of the same input fields (see below). I have a script that will process the input data, but only if it is submitted one set at a time(also below). I will like to have one submit button at the button of the form that would execute the script for however many multiples of the data set I have on the page.
Does anyone have any idea how I can get this to work? I cannot rename the variables (I tried that) because the receiving server needs the data labels to appear exactly as they are below. Any help/direction would be greatly appreciated. Thanks!
My form:
<form method="post" action="<?php echo (AUTHORIZENET_SANDBOX ? AuthorizeNetDPM::SANDBOX_URL : AuthorizeNetDPM::LIVE_URL)?>" id="checkout_form">
<?php
$time = time();
$fp_sequence = $time;
$fp = AuthorizeNetDPM::getFingerprint(AUTHORIZENET_API_LOGIN_ID, AUTHORIZENET_TRANSACTION_KEY, $amount, $fp_sequence, $time);
$sim = new AuthorizeNetSIM_Form(
array(
'x_amount' => $amount,
'x_fp_sequence' => $fp_sequence,
'x_fp_hash' => $fp,
'x_fp_timestamp' => $time,
'x_relay_response'=> "TRUE",
'x_relay_url' => $coffee_store_relay_url,
'x_login' => AUTHORIZENET_API_LOGIN_ID,
'x_test_request' => TEST_REQUEST,
)
);
echo $sim->getHiddenFieldString();
}
?>
<fieldset>
<H3>Card #1</H3>
<div>
<label>Amount</label>
<input type="text" class="text required" size="4" name="amount" value=""></input>
</div>
<div>
<label>Credit Card Number</label>
<input type="text" class="text required creditcard" size="15" name="x_card_num" value="6011000000000012"></input>
</div>
<div>
<label>Exp.</label>
<input type="text" class="text required" size="4" name="x_exp_date" value="04/15"></input>
</div>
<div>
<label>CCV</label>
<input type="text" class="text required" size="4" name="x_card_code" value="782"></input>
</div>
</fieldset>
<fieldset>
<div>
<label>First Name</label>
<input type="text" class="text required" size="15" name="x_first_name" value="John"></input>
</div>
<div>
<label>Last Name</label>
<input type="text" class="text required" size="14" name="x_last_name" value="Doe"></input>
</div>
</fieldset>
<fieldset>
<div>
<label>Address</label>
<input type="text" class="text required" size="26" name="x_address" value="123 Four Street"></input>
</div>
<div>
<label>City</label>
<input type="text" class="text required" size="15" name="x_city" value="San Francisco"></input>
</div>
</fieldset>
<fieldset>
<div>
<label>State</label>
<input type="text" class="text required" size="4" name="x_state" value="CA"></input>
</div>
<div>
<label>Zip Code</label>
<input type="text" class="text required" size="9" name="x_zip" value="94133"></input>
</div>
<div>
<label>Country</label>
<input type="text" class="text required" size="22" name="x_country" value="US"></input>
</div>
</fieldset>
<fieldset>
<H3>Card #2</H3>
<div>
<label>Amount</label>
<input type="text" class="text required" size="4" name="amount" value=""></input>
</div>
<div>
<label>Credit Card Number</label>
<input type="text" class="text required creditcard" size="15" name="x_card_num" value="6011000000000012"></input>
</div>
<div>
<label>Exp.</label>
<input type="text" class="text required" size="4" name="x_exp_date" value="04/15"></input>
</div>
<div>
<label>CCV</label>
<input type="text" class="text required" size="4" name="x_card_code" value="782"></input>
</div>
</fieldset>
<fieldset>
<div>
<label>First Name</label>
<input type="text" class="text required" size="15" name="x_first_name" value="John"></input>
</div>
<div>
<label>Last Name</label>
<input type="text" class="text required" size="14" name="x_last_name" value="Doe"></input>
</div>
</fieldset>
<fieldset>
<div>
<label>Address</label>
<input type="text" class="text required" size="26" name="x_address" value="123 Four Street"></input>
</div>
<div>
<label>City</label>
<input type="text" class="text required" size="15" name="x_city" value="San Francisco"></input>
</div>
</fieldset>
<fieldset>
<div>
<label>State</label>
<input type="text" class="text required" size="4" name="x_state" value="CA"></input>
</div>
<div>
<label>Zip Code</label>
<input type="text" class="text required" size="9" name="x_zip" value="94133"></input>
</div>
<div>
<label>Country</label>
<input type="text" class="text required" size="22" name="x_country" value="US"></input>
</div>
</fieldset>
<input type="submit" value="BUY" class="submit buy">
</form>
My script:
<?php
require_once 'coffee_store_settings.php';
if ($METHOD_TO_USE == "AIM") {
$transaction = new AuthorizeNetAIM;
$transaction->setSandbox(AUTHORIZENET_SANDBOX);
$transaction->setFields(
array(
'amount' => $amount,
'card_num' => $_POST['x_card_num'],
'exp_date' => $_POST['x_exp_date'],
'first_name' => $_POST['x_first_name'],
'last_name' => $_POST['x_last_name'],
'address' => $_POST['x_address'],
'city' => $_POST['x_city'],
'state' => $_POST['x_state'],
'country' => $_POST['x_country'],
'zip' => $_POST['x_zip'],
'email' => $_POST['x_email'],
'card_code' => $_POST['x_card_code'],
)
);
$response = $transaction->authorizeAndCapture();
if ($response->approved) {
// Transaction approved! Do your logic here.
header('Location: thank_you_page.php?transaction_id=' . $response->transaction_id);
} else {
header('Location: error_page.php?response_reason_code='.$response->response_reason_code.'&response_code='.$response->response_code.'&response_reason_text=' .$response->response_reason_text);
}
} elseif (count($_POST)) {
$response = new AuthorizeNetSIM;
if ($response->isAuthorizeNet()) {
if ($response->approved) {
// Transaction approved! Do your logic here.
// Redirect the user back to your site.
$return_url = $site_root . 'thank_you_page.php?transaction_id=' .$response->transaction_id;
} else {
// There was a problem. Do your logic here.
// Redirect the user back to your site.
$return_url = $site_root . 'error_page.php?response_reason_code='.$response->response_reason_code.'&response_code='.$response->response_code.'&response_reason_text=' .$response->response_reason_text;
}
echo AuthorizeNetDPM::getRelayResponseSnippet($return_url);
} else {
echo "MD5 Hash failed. Check to make sure your MD5 Setting matches the one in config.php";
}
}
PHP supports array notation for form fields, so you can force values to show up multiple times. e.g.
<input type="text" name="data[1]" value="foo" />
<input type="text" name="data[a]" value="bar" />
<input type="text" name="data[abcef]" value="baz" />
would give you a $_POST structure like:
$_POST['data'] = array(
0 => 'foo'
'a' => 'bar'
'abcef' => 'baz'
)
Since you'd have multiple copies of multiple fields, all requiring the data to be kept together, then structure your form like:
Set #1
<input name="somefield[1]" />
<input name="otherfield[1]" />
...
Set #2
<input name="somefield[2]" />
<input name="otherfield[2]" />
and then do this on the server upon submission:
foreach(array_keys($somefield) as $key) {
$somefield = $_POST['somefield'][$key];
$otherfield = $_POSt['otherfield'][$key];
... do something ...
}
You could add the form inputs in an array and use an foreach when you run the code.
<input type="text" class="text required" size="4" name="cards[0][amount]" value=""></input>
<input type="text" class="text required creditcard" size="15" name="cards[0][x_card_num]" value="6011000000000012"></input>
<input type="text" class="text required" size="4" name="cards[1][amount]" value=""></input>
<input type="text" class="text required creditcard" size="15" name="cards[1][x_card_num]" value="6011000000000012"></input>
etc etc etc.
You can get the results by doing
foreach($_POST['cards'] AS $c) {
echo $c['amount'];
}

Categories