PHP will not connect with mysql database - php

Can anyone see a reason why this would be failing to connect with / write to a mysql database? I can't be sure which but I know the data isn't entering when the form is submitted. It just routes to a blank screen.
The html
<html>
<div style="width: 330px; height: 130px; overflow: auto;">
<form STYLE="color: #f4d468;" action="send_post.php" method="post">
Category: <select STYLE="color: #919191; font-family: Veranda;
font-weight: bold; font-size: 10px; background-color: #000000;"
name="category">
<option value="blah1">blah1</option>
<option value="blah2">blah2</option>
<option value="blah3">blah3</option>
<option value="blah4">blah4</option>
</select> <br>
<textarea overflow: scroll; rows="4" cols="60" STYLE="color:
#919191; font-family: Veranda; font-weight: bold; font-size: 10px;
background-color: #000000; width:300px; height:80px; margin:0;
padding:0;" name="contents">
</textarea><br>
<input type="submit" STYLE="color: #919191; font-family: Veranda;
font-weight: bold; font-size: 10px; background-color: #000000;"
value="Create Log">
</form>
</div>
</html>
and send_post.php
<?php
//Connecting to sql db.
$connect=mysqli_connect("localhost","username","password","mysqldatabasename");
//Sending form data to sql db.
mysqli_query($connect,"INSERT INTO mytablename (category, contents)
VALUES ('$_POST[category]', '$_POST[contents]')";
?>
I'm new to this but I researched hard and now I'm at an impasse. I've tried entering code that checks for connection errors but I always just get a white screen and no db updates. I would greatly appreciate help. (Also do the quotes stay included in "localhost", "username" etc?)

Seems to be a simple syntax error.
Missing a closing ) here:
mysqli_query($connect,"INSERT INTO mytablename (category, contents) VALUES ('$_POST[category]', '$_POST[contents]')");
The arguments for the mysqli_connect do need to be enclosed in quotes. You also might want to seriously consider sanitizing your inputs. This form is wide open.

Related

PHP posting error

I keep getting a Cannot POST /app/newpost/newpost.php error when i try to post a HTML form and retrieve the data from another HTML page
HTML for the form is:
<div style="text-align:center">
<h1 style="font-size:36px; font-family:verdana;">
Create a new forum post
</h1>
</div>
<form action="newpost.php" method="POST" style="text-align:center">
<P style="font-size:24px; font-family:verdana;">Title: <input type="text" name="title" style="font-size:24px; font-family:verdana;"></P>
<P style="font-size:24px; font-family:verdana;">Description: <input type="text" name="desc" style="font-size:24px; font-family:verdana;"></P>
<input type="submit" action="window.location.href='/app/forum/forum.component.html'" style="background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: left; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer;">
</form>
PHP file is
<?php
if (isset($_POST["submit"]))
{$title = $_POST['title'];
$desc = $_POST['desc']
}
?>
im still very new to PHP someone pls help!
And how to retrieve the posted data on another html page?
You need a web server with PHP installed to test/run your PHP script. You cannot have it run from your file system directory. Depending on your environment you might want to installed something called WAMP, XAMP or MAMP.
Looks like there ; missing
i have changed you code to bellow, it should run now
<?php
if (isset($_POST["submit"]))
{
$title = $_POST['title'];
$desc = $_POST['desc'];
}
?>
try this
I think having action="newpost.php" as an attribute in the form tag and action="window.location.href='/app/forum/forum.component.html'" as an attribute in the input type="submit" tag is a contradiction. You might want to try to erase one of them.

Wordpress Template causes 404 Error

I've created a page in Wordpress and used this form as template. But when I submit the form it shows Error 404 page not found.
I've tried various scripts in php but I didnt get fruitful results. Hoping for the best answers. Thanks :)
Here is my coding
<?php
/*
template name: visit
*/
$servername= "localhost";
$username="root";
$password="";
$database="wp_chandan";
if(isset($_POST['name']) && isset($_POST['email']))
{
$name=$_POST['name'];
$email=$_POST['email'];
var_dump($email);
#mysql_connect($servername,$username,$password);
#mysql_select_db($database);
$query=mysql_query("insert into visitor_d (NAME,Email) values ('$name','$email') ");
} ?>
<html>
<head>
<?php get_header(); ?>
<style type='text/css'>
/* form elements */
form {
margin:100px;
padding: 10px 2px;
background: #F5F5F5;
width: 50%;
height:auto;
border:1px solid green;
}
input {
padding:2px;
border:1px solid #eee;
font: normal 1em Verdana, sans-serif;
color:#777;
align:center;
}
input.button {
font: bold 12px Arial, Sans-serif;
height: 24px;
margin: 0;
align:center;
padding: 2px 3px;
color: #333;
background: #e7e6e6 url(MarketPlace-images/button.jpg) repeat-x;
border: 1px solid #dadada;
}
</style>
</head>
<form action="" method="POST">
<fieldset>
<legend><b><u>Visitor Details:</u></b></legend>
Name: <input type="text" name="name" placeholder="Fill Name" required><br><br>
Email: <input type="text" name="email" required placeholder="Enter Email"><br><br>
<input type="submit" name="submit"><br>
</fieldset>
</form>
<?php get_footer();?>
</html>
Make sure that you don't have any reserved WordPress terms in the name attributes in the fields of the form.
WordPress uses many of these reserved terms for it's internal rewriting and as a result if you using name attributes such as "name", it breaks the WordPress rewrite rules and causes this 404 Not Found page after submitting the form.
See more here: http://www.lost-in-code.com/platforms/wordpress/wordpress-404-not-found-when-submitting-form/
html5 doesn't like the empty action attribute, it does cause issues in some browsers. you have not selected a standard so i assume the browser will default to html5
<form action="#" method="POST">
Setting the action as # should work but one thing to watch out for is actions added before your page template is loaded, this can obviously grab the $_POST and redirects would be common so that may also be a reason.
also
Name: <input type="text" name="name" placeholder="Fill Name" required>
WP does not like when you call a field "name" it conflicts with something (i have forgotten the reason by now)
Name: <input type="text" name="aname" placeholder="Fill Name" required>

How To Post Data To Some External Php Without Redirecting & How To Loop Or Execute Some Php For Several Times

I'm trying to create sms floader and i have created the two following php files but when ever the user entered his mobile number its redirecting to another external website and please help me on how to get input from user to loop the same code for several times. like if the user they enter his mobilenumber and loop value to 10 times it should send 10 sms to destination number
sms.php
<html>
<head>
<meta charset="utf-8">
<title>TrickzTracking | SMS Floader</title>
<meta name="author" content="Nandan T Rsn" />
<meta name="keyword" content="sms bomber,sms bomber 2016,working sms bomber,trickztracking sms bomber,unlimited sms bomber,anonymous sms bomber" />
<meta name="description" content="Free SMS Bomber | TrickzTracking" />
<meta name="contact" content="TrickzTracking#Gmail.com" />
<meta name="copyright" content="Copyright (c)2015-2016
TrickzTracking. All Rights Reserved." />
<style>
body {
background: #3b5998;
font-family: 'Lato', sans-serif;
color: #FDFCFB;
text-align: center;
}
#id1{
color: #000000;
}
input{
margin: 5px;
}
.input{
margin: 5px;
}
form {
width: 500px;
margin: 17% auto;
margin-top:90px;
}
.header {
font-size: 35px;
text-transform: uppercase;
letter-spacing: 5px;
}
.description {
font-size: 20px;
letter-spacing: 1px;
line-height: 1.3em;
margin: -2px 0 45px;
}
.button {
padding: 10px;
height: 44px;
border: none;
}
#email,#number,#message,#times {
width: 70%;
background: #FDFCFB;
font-family: inherit;
color: #737373;
letter-spacing: 1px;
text-indent: 5%;
border-radius: 5px 0 0 5px;
}
#submit {
width: 31%;
padding: 10px;
height: 46px;
background: #E86C8D;
font-family: inherit;
font-weight: bold;
color: inherit;
letter-spacing: 1px;
border-radius: 0 5px 5px 0;
cursor: pointer;
transition: background .3s ease-in-out;
}
#submit:hover {
background: #d45d7d;
}
input:focus {
outline: none;
outline: 2px solid #E86C8D;
box-shadow: 0 0 2px #E86C8D;
}
</style>
<link href="http://fonts.googleapis.com/css?family=Lato:400,700"rel="stylesheet" type="text/css">
</head>
<body>
<form action="sms_api.php" method="post" name="SMS Floader">
<div class="header">
<p>Bomb your friends</p>
</div>
<div class="description">
<p>Now, you can bomb your Friends number by simply using their numbers in the below form </p>
</div>
<div class="input">
<input type="text" class="button" id="number" name="num" placeholder="XXXXXXXXXX" maxlength="10" value="">
<button type="submit" class="button" onclick="submit">Start</button>
</div>
<div class="">
<h4> Terms And Conditions : </h4>
</div>
<div class="description">
<p id="id1">1. Don't Try To Fload On Unknow Person's. </p>
<p id="id1">2. Send Only Maximum Of 50 SMS On The Go.</p>
<p id="id1">2. Your IP Will Be Logged For Security Purpose.</p>
<p>Powered By: www.TrickzTracking.com</p>
</div>
</form>
</body>
</html>
sms_api.php
<html>
<script type="text/javascript">
function submit(myForm){
document.myForm.submit();
}
</script>
<body onLoad="javascript:submit(myForm)">
<form action="http://www.some-other-website.com/sms/api.php" method="post" name="myForm">
<input type="hidden" name="type" value="0">
<input type="hidden" name="message" value="102310">
<input type="hidden" name="num" value="<?php echo $_POST['num'] ?>">
<input name="hidden" type="submit">
</form>
</body>
</html>
Ps: when ever sms.php send post data to sms_api.php its redirecting it to http://www.some-other-website.com/sms/api.php instead i want that to redirect to sms.php after posting data to http://www.some-other-website.com/sms/api.php
I'm new to php please help me :D
You are using a on load submit function, which will submit the form as soon as the
sms_api is called. The coding format is wrong and so is the concept. You should try making a single form rather than trying to complex the code. Keep it simple.

MySql/PHP - How can I insert data into 2 different tables at the same time (using Array)

I have a form that creates request. By clicking on the 'Create Request' button all the information is added to the 'Requests_Table'. This is working perfectly. Each request has more than one item (at least one)... and each item is comprised of its own characteristics (that's why I want to store this info on a separate table 'SerialNumber_Table'.
So, I need help inserting the Serial number information (Type,Model,Serial number) into 'SerialNumber_Table'. I believe I'll also need to use mysql_insert_id(); to associate each serial number with the correct request_id.
`
//Variables for Serial Numbers
$machineType = mysql_prep($_POST['machineType']);
$machineModel = mysql_prep($_POST['machineModel']);
$serialNumber = mysql_prep($_POST['serialNumber']);
<fieldset class="row2">
<h1><?php echo $LANG['hardware_details']; ?></h1>
<p style="margin: 10px 0 10px 0; ">
<p style="margin: 10px 0 10px 0; font-size:12px; padding: 0;"><?php echo $LANG['delete_machines']; ?></p>
<input type="button" value="<?php echo $LANG['add_machines']; ?>" onClick="addRow('dataTable')" style="width: 100px; padding: 2px 5px 2px 5px; background: #e1e1e1; color: #000; font-weight: bold; font-size: 12px; cursor: pointer;" />
<input type="button" value="<?php echo $LANG['remove_machines']; ?>" onClick="deleteRow('dataTable')" style="width: 130px; margin-left: 10px; padding: 2px 5px 2px 5px; background: #e1e1e1; color: #000; font-weight: bold; font-size: 12px; cursor: pointer;" />
</p>
<table id="dataTable" class="form" border="1">
<tbody>
<tr>
<td><input type="checkbox" style=" width: 30px; "/></td>
<td>
<label style="margin-left: 10px;"><?php echo $LANG['machineType']; ?></label>
<input type="text" value="" id="machineType" name="machineType[]" style="width: 70px; margin: 5px 10px 10px 10px; ">
</td>
<td>
<label for="" style="margin-left: 10px;"><?php echo $LANG['machineModel']; ?></label>
<input type="text" value="" id="machineModel" name="machineModel[]" style="width: 70px; margin: 5px 10px 10px 10px;">
</td>
<td>
<label for="" style="margin-left: 10px;"><?php echo $LANG['serialNumbers']; ?></label>
<input type="text" value="" id="serialNumber" name="serialNumber[]" style="width: 120px; margin: 5px 10px 10px 10px;">
</td>
</tr>
</tbody>
</table>
<div class="clear"></div>
</fieldset>
<!-- End Dynamic Forms for Serial Numbers -->
`
How could I achieve such a task?
If it is any help, I have posted the entire code for this page at http://aerco.com.br/stackoverflow/source.txt
Anyone can point me in the right direction here?
Thanks in advance for any thoughts
I could not get what is yout exact need, but for my understanding, You can insert into one table using code and another using MySQL trigger.
Explained over here: http://dev.mysql.com/doc/refman/5.0/en/triggers.html
would the user be the one to supply the serial or is it predefined? this should be the code you need for inserting
$query = "INSERT INTO serialnumber_table(Type,Model,Serial number,request_id) VALUES ($machineType, $machineModel,$serial_number,request_id)
request_id should be gotten from requests table.
if my understanding is correct, insert after this code
$query = "INSERT INTO requests(
request_date, requester_name, requester_email, client_name, client_country, opportunity_number, machine_quantity, severity, specialist, created_by, sales_connect
) VALUES (
'{$request_date}', '{$requester_name}', '{$requester_email}', '{$client_name}', '{$client_country}', '{$opportunity_number}', '{$machine_quantity}', '{$severity}', '{$specialist}', '{$created_by}', '{$sales_connect}'
)";

Onclick submit, in a form, get inputbox value into variable and output it into another inputbox

I am developping a job appliance form, that outputs its results into a CRM (Customer Relationship Management).
My problem is that my client wants the fields "Street Adress, Postal Code and locality" outputted all together in one single field in CRM, after inputted in different fields on the form.
So i thought i should use php for when the user clicks on submit it gets the value of each one of the fields and inputs it into an hidden field i created.
for some reson, as i am not a php expert, it isn't working.
It is really not outputting any value into the CRM
this is what i've got so far:
<form action="https://service.capsulecrm.com/service/newlead" method="post">
<input name="morada" id="morada" maxlength="255" title="" style="border-style: solid; border-width: 1px; border-color: #cccccc; position: absolute; margin: 0px; padding: 0px; width: 220px; height: 20px; font-family: Tahoma; font-size: 11px; color: #333333; background-color: #ffffff; z-index: 1; left: 189px; top: 397px;" value="" type="text">
<input name="postal" id="postal" maxlength="255" title="" style="border-style: solid; border-width: 1px; border-color: #cccccc; position: absolute; margin: 0px; padding: 0px; width: 220px; height: 20px; font-family: Tahoma; font-size: 11px; color: #333333; background-color: #ffffff; z-index: 1; left: 189px; top: 437px;" value="" type="text">
<input name="local" id="local" maxlength="255" title="" style="border-style: solid; border-width: 1px; border-color: #cccccc; position: absolute; margin: 0px; padding: 0px; width: 220px; height: 20px; font-family: Tahoma; font-size: 11px; color: #333333; background-color: #ffffff; z-index: 1; left: 189px; top: 467px;" value="" type="text">
<?php if ($_POST['submit']) {
$addressmorada = $_POST['morada'];
$addresspostal = $_POST['postal'];
$addresslocal = $_POST['local'];
echo "<input type='hidden' name='STREET' value='".$addressmorada. " " .$addresspostal. " " .$addresslocal."' />";
}
?>
<input name="submit" type="submit" value="Submit" style="
border-style: none; border-width: 0px; border-color: #888888; position: absolute; margin: 0px; padding: 0px; width: 93px; height: 33px; z-index: 1;
left: 433px; top: 1773px;
text-indent: 99999999999999px;
background: green;
cursor: pointer;
">
</form>
I will try to explain my PHP code,
the if statement it's calling the function on submit.
the variables get the different input values.
I echo an input field type hidden, calling the variables, so that all the information is outputted into the CRM in one single field
I think the major problem may be on number 3! i'l apreciate any help.
Sory if my english is bad, it's not my mother language.
two issues:
1,
if ($_POST['submit'])
will throw an error: Notice: Undefined index: submit in ... if there is no $_POST data.
it should be changed to
if (isset($_POST['submit']))
2, $_POST['submit'] will never occur as there is no name attribute called submit. You should add this to your "submit" button:
<input type="submit" value="Submit" name="submit" style="[STYLES_HERE]">
You don't have a name for you button right.
<input type="submit" value="Submit" name = "submit" >
You have
if ($_POST['submit']).
So the name of the button should be submit. Otherwise it will never go in your if statement.
For debugging purpose
Try just printing all the values in the if statement. Its helpful.
You're confusing server-side and client-side code. Here's a simple explanation, though you should document yourself more about HTTP and the way web servers work:
when a user A requests a page, a HTTP request is sent to the server
this request is handled by a web server B
according to some rules (file extension, file location, ...), the web server can decide to simply fetch the file and send it to the client A (e.g. for a plain HTML file), or run it through a (PHP) interpreter first and then send the result to the client.
in any case, the client A receives only the HTML: the PHP has already been executed by B at this point.
So the user requests the page with the form. At this point he didn't submit anything yet, so the PHP interpreter gets nothing in $_POST and the hidden input is empty.
The user fills the form and submit it. That triggers a new HTTP request with the data. But of course the hidden input is still empty, and will never get filled because the PHP has already been executed (and won't ever be client-side).
What you need to do is either
modify the target script (https://service.capsulecrm.com/service/newlead) if you have access to it, in order to join the fields together
if you can't, add an intermediate PHP script which is the target of the form, join the fields together and send a HTTP request itself (e.g. with curl) to newlead
or if you don't want to spend too much time on that, use client-side code, that is Javascript, to fill the hidden input. However you can't be sure the form will be submitted correctly (the user can choose to disable Javascript, or the client may not handle it at all: this is common for Braille devices).

Categories