wordpress run php file in form action - php

I am trying to re-do my site to wordpress, I have this problem. I have a form and on a submit php file runs and adds data to database. How do I run php file in wordpress?
<form method="POST" action="insert.php">
<p>
<label for="XX">XX</label>
<input type="text" name="XX" id="XX">
<label for="X">X</label>
<input type="text" name="X" id="X">
<label for="XXY">XXY</label>
<input type="text" name="XXY" id="XXY">
</p>
<input type="submit" value="add to database">
</form>
How do I run the php insert.php file? Thank you!
Or on the other hand, could I write in action the directory to the file? eg. wp-content/myphpfiles/insertgolf.php, would that work? Thank you, I am new to wordpress

In WordPress form page, you can not put php file path. If you want to do this then you need to create a template.
Please follow reference link, here you can know how to create template in WordPress: https://www.cloudways.com/blog/creating-custom-page-template-in-wordpress/
In template file put your code:
<?php /* Template Name: insertGolf */
if(!empty($_POST) && $_SERVER['REQUEST_METHOD'] == 'POST'){
//do database related code here
$link = mysqli_connect("c108um.DDD.com","f96860","qt3C2EQ","f96860");
if($link === false){
die("ERROR: " . mysqli_connect_error());
}
$datum = mysqli_real_escape_string($link, $_REQUEST['datum']);
$odkud = mysqli_real_escape_string($link, $_REQUEST['odkud']);
$sql = "INSERT INTO golf (datum, odkud) VALUES ('$datum', '$odkud')";
if(mysqli_query($link, $sql)){ header('Location: golf.php');
exit;
}
else{
echo "ERROR: " . mysqli_error($link);
}
mysqli_close($link);
}
?>
<form method="POST" action="">
<p>
<label for="XX">XX</label>
<input type="text" name="XX" id="XX">
<label for="X">X</label>
<input type="text" name="X" id="X">
<label for="XXY">XXY</label>
<input type="text" name="XXY" id="XXY">
</p>
<input type="submit" value="add to database">
</form>

Related

Problem: Part of the PHP exercise does not run correctly

I created a section for editing. When I edit the information and click the save button, the information is not saved and the header section does not display completely.
<?php
if (isset($_POST['submit_btn']))
{
$id = $_POST['id'];
$fn = trim($_POST['name']);
$ln = trim($_POST['lastname']);
$age = trim($_POST['age']);
$q = "UPDATE `users` SET `fn` = '$fn',
`ln` = '$ln',
`age` = '$age'
WHERE id = '$id'";
mysqli_query($dbconnect,$q);
if (mysqli_affected_rows($dbconnect) > 0)
redirect("?msg=ok&id=**$id**");
else
redirect("?msg=error&id=**$id**");
}
else
echo ("Not In If(isset)");
?>
<form action="" method="post">
<label for="name">FirstName:</label>
<input type="text" name="name" id="name" value="<?php echo $row['fn']?>">
<br>
<label for="lastname">LastName:</label>
<input type="text" name="lastname" id="lastname" value="<?php echo $row['ln']?>">
<br>
<label for="age">Age:</label>
<input type="text" name="age" id="age" value="<?php echo $row['age']?>">
<br>
<input type="submit" name="submit_btn" value="Save">
<a href="index2.php">
Back
</a>
</form>
</body>
Bold sections do not work here.
Below is a picture of the result:
In the link that I specified, after clicking on save the ID will not be displayed and all the information filled in the forms will be lost.
Sorry if the result is styleless and boring and I just created this page to practice php😁
Thank you for being responsive🙏🙏🙏
You are mistaking a POST request with a GET request.
Part, which appears in the URL is sent to the webserver in GET request.
Your form is submitting POST request to the webserver, logic in the code does the same, but you are trying to display information from url (GET).
Please check the examples in php.net:
POST variables: https://www.php.net/manual/en/reserved.variables.post.php
GET variables: https://www.php.net/manual/en/reserved.variables.get.php
You can take an example with GET request variable below, however, be careful with trusting the "end client" and always prepare your statements, which you send to your database to execute queries.
if (isset($_GET['submit']))
{
$number = $_GET['number'];
echo $number
? "Number which was submitted: $number <br>"
: 'Number is not set';
} else {
echo 'Form has not been yet submitted';
}
?>
<form action="" method="get">
<input type="number" name="number" placeholder="Number">
<input type="submit" name="submit" value="Save">
</form>

Failing to send checkbox value to MySQL database?

I'm trying to store a Boolean value in my database based off a checkbox(Watched) in a form. If the checkbox isn't checked on form submit, it should send a 0; Otherwise a 1.
However, it sends a value of 0 no matter if it's checked or not. The Title Field and Genre field send perfectly, however the Watched conditional is not working correctly. I'm stumped and having a hard time wrapping my head around this, any thoughts?
<?php
require 'config/config.php';
require 'config/db.php';
// Check for Submit
if(isset($_POST['submit'])) {
// Get Form Data
$title = mysqli_real_escape_string($conn, $_POST['title']);
$genre = mysqli_real_escape_string($conn, $_POST['genre']);
if (isset($_POST['watched']) && ($_POST['watched'] == "value")) {
$watched = 1;
} else {
$watched = 0;
}
$query = "INSERT INTO movies(name, genre, watched) VALUES('$title', '$genre', '$watched')";
if (mysqli_query($conn, $query)) {
header('Location: '.ROOT_URL.'');
} else {
echo "ERROR: " . mysqli_error($conn);
}
}
?>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<div class="md-form">
<input type="text" id="form1" class="form-control" name="title">
<label for="form1" class="">Title</label>
</div>
<div class="md-form">
<input type="text" id="form1" class="form-control" name="genre">
<label for="form1" class="">Genre</label>
</div>
<div class="form-group">
<input type="checkbox" id="checkbox1" name="watched">
<label for="checkbox1">Have you watched it already?</label>
</div>
<input type="submit" name="submit" value="submit" class="btn btn-primary">
</form>
You never set an actual value for the checkbox in your form HTML, so the value of the checkbox will never exist which means your if statement will always return false.
Simply change this:
<input type="checkbox" id="checkbox1" name="watched">
to this:
<input type="checkbox" id="checkbox1" name="watched" value="value">

i want to create a php blog system without database

i am creating an blog system where i can use only static files . I have section where i am creating a new post and i write it on a text file like this:
new_post. php
<form id=" form1" name="form1" method="post" action="writeblog.php">
<p>
<label>TITLE: </label><input type="text" name="title"/>
<label for="body"> Body: </label>
<textarea name="body" cols=50 rows=10></textarea>
<br />
<input type="submit" name="submit" value="POST"/>
</form>
writeblog.php
<?php
$out = fopen("blog.txt","a");
if(!$out){
header('Location: new_post.php');
exit;
}
$title=$_POST[title];
$body=$_POST[body];
$body=str_replace("\n"," ",$body);
fputs($out,"\n");
fputs($out,date("m/j/y h:i \n ",time()));
fputs($out,"$title\n\n\n");
fputs($out,"$body\n\n\n\n\n");
fclose($out);
header('Location: new_post.php');
exit;
?>
I need some help with how I can work with just one post from the file delete it or edit it insert in on page.

connect form page on wordpress with php file

I created form on one page in my wordpress:
<form action="scores_send.php" method="post">
<strong>Date: </strong><input type="date" name="date" /><br />
<strong>TEAM 1:</strong>
Player 1.: <input type="text" name="team1_player1" /><br />
Player 2.: <input type="text" name="team1_player2" /><br />
<strong>TEAM 2:</strong>
Player 1.: <input type="text" name="team2_player1" /><br />
Player 2.: <input type="text" name="team2_player2" /><br />
<strong>SCORE:</strong>
Team 1. <input type="number" name="score_team1" min="0" max="5"/> : <input type="number" name="score_team2" min="0" max="5"/> Team 2.<br />
<input type="submit" value="Add" />
</form>
And also I wrote php code which connect to my external (no-wordpress) postgres database (connection parameters in code are not true) and insert information from form to one table:
<?php
$date = $_POST['date'];
$t1p1 = $_POST['team1_player1'];
$t1p2 = $_POST['team1_player2'];
$t2p1 = $_POST['team2_player1'];
$t2p2 = $_POST['team2_player2'];
$score1 = $_POST['score_team1'];
$score2 = $_POST['score_team2'];
if($date and $t1p1 and $t1p2 and $t2p1 and $t2p2 and $score1 and $score2) {
$connection = pg_connect("host=127.0.0.1 port=5432 dbname=scores user=user password=pass")
or die('Brak polaczenia z serwerem PostgreSQL');
$db = #pg_select_db('scores', $connection)
or die('Nie moge polaczyc sie z baza danych');
$team1 = #pg_query("INSERT INTO scores.games (score_team1, score_team2, datetime, team1_player1, team1_player2, team2_player1, team2_player2) VALUES ('$score1', '$score2', '$date', '$t1p1', '$t1p2', '$t2p1', '$t2p2'");
if($team1) echo "Scores added.";
else echo "ERROR! Scores not added";
pg_close($connection);
}
?>
I tried 2 ways:
1. to create template php file in folder of my theme and
2. to paste this code to content editor in wordpress (using this plugin: https://wordpress.org/plugins/insert-php/)
and both ways is not working. I changed part "action" in , but maybe wrong.
Just change your action of your form to : http://yoursite.com/scores_send.php
make sure that your scores_send.php is on the root of your installation.
So, your form look like :
<form action="http://yoursite.com/scores_send.php" method="post">
You should use http://example.com/page-slug/ in form action
Where page-slug is the slug of another page create by make scores_send.php as template.It will post all form value to score_send.php.From where you can proceed further to insert into external DB.
<form action="http://example.com/page-slug/" method="post">

Form resubmission issue PHP SQL

If I insert a new record into the database via PHP and then click the submit button and then each time I refresh the page it inserts the recently added record into the database. How do I stop this from happening? I don't want to redirect to another form after the submit button too. The weird thing is, if I click the submit button twice and then refresh the form it doesn't insert duplicates into the database. Why is this? Please help :/ thanks
Here's the code:
<div class="insertDiv">
<form method="POST" action="contracts.php">
<?php
if(empty($_POST['ContractDate']) && empty($_POST['ComputerId2']) && empty($_POST['CustomerId']) && empty($_POST['ContractLevel']))
{
}
else
{
include("dbinfo.inc.php");
$comm=#mysql_connect(localhost,$username,$password);
$rs=#mysql_select_db($database) or die( "Unable to select database");
$contractDate=$_POST['ContractDate'];
$computerID=$_POST['ComputerId'];
$customerID=$_POST['CustomerId'];
$contractLevel=$_POST['ContractLevel'];
$sql="INSERT INTO contract VALUES ('','$contractDate','$computerID', '$customerID', '$contractLevel')";
$result=mysql_query($sql)or die("Insert Error: ".mysql_error());
mysql_close();
}
?>
<div class = "myButton">
Insert
</div>
<p></p>
Enter contract start date:&nbsp
<input type="date" name="ContractDate" size=30 class="input"><br><br>
Enter computerID:&nbsp
<input type="text" name="ComputerId" size=30 class="input"><br><br>
Enter customerID:&nbsp
<input type="text" name="CustomerId" size=30 class="input"><br><br>
Enter contract level:&nbsp
<input type="text" name="ContractLevel" size=30 class="input"><br><br>
<input type="reset" value="Reset" class="button">&nbsp&nbsp&nbsp&nbsp
<input type="submit" value="Submit" class="button">
</form>
</div>
check with some value from form which is already exists in table if no then insert the data into the table
or
1.generate a random string and store it in session,
2.then output it to your form as a hidden value,
3.check the submitted and store variable, if matches process your request,
4.go to 1.
Just make sure the form has only been submitted once, by using sessions.
This should work (I haven't been able to test it)
<?php
// you should start the session before sending any output to the browser
session_start();
if($_SESSION['prevent'] && $_SESSION['prevent'] == $_POST['prevent'])$valid=true;
$_SESSION['prevent']=sha1(mt_rand(0,9999999));
?>
<div class="insertDiv">
<form method="POST" action="contracts.php">
<?php
if(empty($_POST['ContractDate']) && empty($_POST['ComputerId2']) && empty($_POST['CustomerId']) && empty($_POST['ContractLevel']))
{
}
else
{
if($valid){
include("dbinfo.inc.php");
$comm=#mysql_connect(localhost,$username,$password);
$rs=#mysql_select_db($database) or die( "Unable to select database");
$contractDate=$_POST['ContractDate'];
$computerID=$_POST['ComputerId'];
$customerID=$_POST['CustomerId'];
$contractLevel=$_POST['ContractLevel'];
$sql="INSERT INTO contract VALUES ('','$contractDate','$computerID', '$customerID', '$contractLevel')";
$result=mysql_query($sql)or die("Insert Error: ".mysql_error());
mysql_close();
}
}
?>
<div class = "myButton">
Insert
</div>
<p></p>
Enter contract start date:&nbsp
<input type="date" name="ContractDate" size=30 class="input"><br><br>
Enter computerID:&nbsp
<input type="text" name="ComputerId" size=30 class="input"><br><br>
Enter customerID:&nbsp
<input type="text" name="CustomerId" size=30 class="input"><br><br>
Enter contract level:&nbsp
<input type="text" name="ContractLevel" size=30 class="input"><br><br>
<input type="reset" value="Reset" class="button">&nbsp&nbsp&nbsp&nbsp
<input type="hidden" value="<?php echo $_SESSION['prevent']; ?>">
<input type="submit" value="Submit" class="button">
</form>
</div>
Have a read of this article and it'll explain how to redirect so refreshing doesn't resubmit the form.
http://en.wikipedia.org/wiki/Post/Redirect/Get

Categories