I have designed my interface for submitting data to my database table but I am unsure as to how to go about doing this here is some of my code most of it is the same so once i know how to do it I can replicate it.
<form method="post" action="input.php">
<?php wp_nonce_field('update-options'); ?>
<table width="600">
<tr valign="top>">
<th width="92" scope="row">Enter New Track</th>
<th width="92" scope="row">Enter New Position</th>
<th width="92" scope="row">Enter New Driver</th>
<th width="92" scope="row">Enter New Class</th>
</tr>
<tr valign="top>">
<td><input type="text" name="track" /></td>
<td><input type="text" name="position" /></td>
<td><input type="text" name="driver" /></td>
<td><input type="text" name="class" /></td>
</tr>
<tr valign="top">
<td><input type="submit" value="Submit" /></td>
<td><input type="submit" value="Submit" /></td>
<td><input type="submit" value="Submit" /></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
what I want to do is input each of these values on there own into there table I have goggled the question and not found anything that was any help any ideas are appreciated.
There are a number of problems here which need solving.
First of all, you only need one <input> of type submit. That can go outside of the table. Secondly, your valign attributes contain an extra > character.
<table width="600">
<tr valign="top">
<th width="92" scope="row">Enter New Track</th>
<th width="92" scope="row">Enter New Position</th>
<thwidth="92" scope="row">Enter New Driver</th>
<th width="92" scope="row">Enter New Class</th>
</tr>
<tr valign="top">
<td><input type="text" name="track" /></td>
<td><input type="text" name="position" /></td>
<td><input type="text" name="driver" /></td>
<td><input type="text" name="class" /></td>
</tr>
</table>
<br />
<input type="submit" value="Submit" />
The above code should go inside your <form> tags.
Once you have submitted the form, the data will be POSTed to input.php, the intent of which is to process the data and insert any new data to your tables (why you're doing it this way still remains a mystery, but I'll answer the question first).
input.php should look something like this:
<?php
// I will ignore error handling here, but if you want a good tutorial on PHP PDOs, try http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html
$pdoObj = new PDO("mysql:host=[yourhost];dbname=[yourdbname]", [db_username], [db_password]);
// keep in mind that this code is WAY OPEN to SQL injection attacks, but this will at least give you an idea of how to get your code to function, if not securely.
if(isset($_POST['track'])) {
$pdoObj->exec("INSERT INTO tracks (name) VALUE ('" . $_POST['track'] . "')");
}
if(isset($_POST['position'])) {
$pdoObj->exec("INSERT INTO positions (name) VALUE ('" . $_POST['position'] . "')");
}
if(isset($_POST['driver'])) {
$pdoObj->exec("INSERT INTO drivers (name) VALUE ('" . $_POST['driver'] . "')");
}
if(isset($_POST['class'])) {
$pdoObj->exec("INSERT INTO classes (name) VALUE ('" . $_POST['class'] . "')");
}
// some code here that will either display some copy or redirect the user to another page
NOTE:
It looks as though you're still new to web development, so I'll leave you with this: SQL Injection is no joke. You *MUST * defend against it otherwise you can lose EVERYTHING in your database.
Pick up a few books or tutorials on PHP, Wordpress and web development, in general. Talk to people in the community about best practices and try to get more pointers on how to do basic tasks you feel you're comfortable with. Then move on to more complex scenarios that test your competence. With enough experience, you'd be amazed at what you can make your web applications do.
Related
I have a form which is dynamic and user can add more rows to insert different hours, I wonder which is the best way to add those data to MySQL database.
<input style="margin-left:28px;" type="image" class="add_field_button" src="img/add.png" />
<table id="dataTable" width="900" style="margin-left:25px; margin-top:10px;">
<tr style="text-align:center;">
<td width="70">From</td>
<td>To</td>
<td>Duration Hours</td>
<td>Code</td>
<td>Remark</td>
</tr>
<tr style="height:85px; text-align:center;">
<td><input type="text" name="hours1[]" id="hours1" /></td>
<td><input type="text" name="hours2[]" id="hours2" /></td>
<td><input type="text" name="durationh[]" id="durationh" /></td>
<td><input type="text" name="hrscode[]" id="hrscode" /></td>
<td><textarea name="remark[]" id="remark"></textarea></td>
</tr>
</table>
Inputs are expandable so I don't know how many rows they will send to save in mysql.
you can use a foreach(), using the key to bind the input groupings together
foreach($_POST['hours1'] as $key => $value) { // could use any of the fields in $_POST['hours1']
//$_POST['hours1'][$key];
//$_POST['hours2'][$key];
//$_POST['durationh'][$key];
//$_POST['hrscode'][$key];
//$_POST['remark'][$key];
//INSERT INTO table (columns) VALUES (your $_POST values)
}
A simple test program program is included.
enter data into dataone
click on create system
dataone data moved to datatwo
works with 5.3
with 5.4 when the create system image is clicked
the data in dataone disappears from the form and noting is passed to datatwo
we have a large number of programs that use this method of input fields and images to click on.
If I shift to 5.3 it works.
If I shift to 5.4 it wil not work.
<!DOCTYPE HTML>
</head>
<?php
$dataone = $HTTP_POST_VARS['dataone'];
$datatwo = $HTTP_POST_VARS['datatwo'];
if(isset($_POST['proc_x']))
{
$datatwo = $dataone;
}
?>
<form name="form1" method="post" action="test.php">
<table>
<tr>
<td><strong>
One</strong>
</td>
</tr>
<tr>
<td colspan="2"><input name="dataone" type="text" id="dataone"
value="<?php echo $dataone?>" size="70">
</td>
</tr>
<tr>
<td><strong>
two</strong>
</td>
</tr>
<tr>
<td colspan="2"><input name="datatwo" type="text" id="datatwo"
value="<?php echo $datatwo; ?>" size="70">
</td>
</tr>
</table>
<table>
<tr>
<td colspan="1"><div align="left" >
<input name="proc" type="image" id="proc" value="proc" alt="Create System"
title="Create System" width="25" height="25">
<strong>proc</strong></div>
</td>
</tr>
</table>
<table>
<tr>
<td colspan="2"><input name="inx" type="hidden" id="inx"
value ="<?php echo $inx; ?>" size="100">
</td>
</tr>
</table>
</form>
</body>
</html>
$HTTP_POST_VARS was deprecated long ago. I haven't actually seen anyone use it in nearly 10 years. Use $_POST instead.
Also, you have opened yourself up to potential XSS attacks. Always use htmlspecialchars() around any arbitrary data used in the context of HTML.
I need another name2 column where in it has a button(copy all) once you click it a dialog box will appear "Are you sure you want to copy |Yes/No". it'll clone the data from name to name2.
<table class="downpanel">
<thead>
<tr>
<th></th>
<th>Name</th>
<th></th>
<th>Name2</th>
<th></th>
<th colspan="">Count</th>
<th></th>
<th>Unit</th>
<th></th>
<th>Category</th>
<th></th>
<th>Data1</th>
<th></th>
<th>Data2</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="button" class="copyall">
</td>
<td>
<input type="text" size="25" name="name">
</td>
<td>
<input type="text" size="25" name="name2">
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<input type="text" size="3" name="from">
</td>
<td>
<input type="text" size="3" name="to">
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<select name="unit" style="width: 75px;">
<option value="blah">blah</option>
</select>
<br />
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<select name="category" style="width: 275px;">
<option value="blah">blah</option>
</select>
<br />
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<input type="text" size="10" id="datepicker" name="data1">
</td>
<td>
<input type="button" class="copy">
</td>
<td>
<input type="text" size="7" name="data2">
</td>
<td>
<input type="button" class="copy">
</td>
</tr>
</tbody>
</table>
http://jsfiddle.net/jakecigar/hq2GG/2/
check this http://jsfiddle.net/hq2GG/4/
var r = confirm("Are you sure you want to copy?");
in both copy and copy all
http://jsfiddle.net/hq2GG/6/
You are facing a mode mash up issue. You initiated your data using HTML and now you want to manipulate it via Javascript. The more complex your issues get, the more you will end up in coding chaos. This is NOT about mixing different coding practices but to break the conceptual rework by designing one part in HTML and the other in Javascript. So as javascript seems to be mandatory here first design everything as if your whole application would only consist of Javascript. Define a mode:
DOM-Node-Manipulation (would be state of the art and also easier for manipulation) OR
html compile style (using innerHTML)
Once you got a mode make your matrix a matrix. If you need to copy cells, respectively rows and columns, you need to be able to address those. In the DOM Model you could maintain a two-dimensional wrapper array, in the html compile style you would obviously fittle with IDs, such as cell1_3, which helps you to make use of the getElementById. At the point in time you are able to address elements, it is just a matter of formulating loops, to get things copied, moved or deleted in bulk mode.
Once you got all of the conceptional prework you can decide to prefill your html page with html (and not javascript) - however this must follow the rules you set yourself for the scripting mode.
Can someone help me please, I am trying to implement a simple php MySQL survey, but I am stuck on this, how do I store the specific selection in MySQL that users select.
How satisfied are you:
<table id="table1" rules="all" border="1" cellpadding="3" cellspacing="0" width="70%">
<colgroup width="70%"></colgroup>
<colgroup width="6%">
</colgroup>
<thead>
<tr align="center">
<th></th>
<th font="" style="font-size:9pt;" size="2"><b>Dissatisfied</b></th>
<th font="" style="font-size:9pt;" size="2"><b>Satisfied</b></th>
<th font="" style="font-size:9pt;" size="2"><b>Very Satisfied</b></th>
</tr>
</thead>
<tbody>
<tr align="center">
<td align="left"><font style="font-size:10pt;"><b>Technician's ability to understand the unique nature of your problem?</b></font></td>
<td><input name="satisfaction" value="Dissatisfied" type="radio"></td>
<td><input name="satisfaction" value="Satisfied" type="radio"></td>
<td><input name="satisfaction" value="Very Satisfied" type="radio"></td>
</tr>
<tr align="center">
</tbody>
</table>
for this what you need will need a form to submit data selection. You will need two php files
The first one is to built a form of radio buttons. I had removed table's elements for neat view then we set the URL of the second page .Can also be a single page if you want but this is more easier.
feedback.php
<form action="add.php" method="post" accept-charset="utf-8">
<input name="satisfaction" value="Dissatisfied" type="radio">Dissatisfied<br>
<input name="satisfaction" value="Satisfied" type="radio">Satisfied<br>
<input name="satisfaction" value="Very Satisfied" type="radio">Very Satisfied<br>
<input type='submit' value="submit">
</form>
In the second page we catch what we had posted from first page.
add.php
$result= $_POST['satisfaction'];
echo $result;
using $result to store in your database. Good luck
As simple as :
1st, Add this line before the table tag:
<form method="post" action="">
2nd, this one at the end of your code:
<input type="submit" name="submit" value="Submit">
</form>
Then you can have this code for inserting in the DB.
if ($stmt = $mysqli->prepare("INSERT INTO TABLENAME(COLUMN NAME) values (?)")) {
$stmt->bind_param('s', $satisfaction);
$satisfaction=$_POST['satisfaction'];
$stmt->execute();
$stmt->close();
}
I hope I can be clear enough in what I need here. What I have is a function that contains some html for an email client app I'm developing. This part of the app uses a common forward, reply and "reply to all" form. Since they are all basically the same, I figured I'd go the lean route and just use a single function to handle this. The only real differences I can see between the 3 actions I mentioned above are that in a reply to all, there will be multiple email addys in the CC part of the form. For a forward, no (cc) and the (to) box should be blank. I need to represent all of this functionality and I'm kind of confused on what the best way to do this is. Can anyone please offer any help? Thanks.
I can certainly post the html is need be, I just wanted to start light.
EDIT:
I almost forgot, there will be POST values when the user submits the form in the event that the form validation fails.
function compose($type,$contents=null)
{
echo'
<tr>
<td>
<tr>
<td valign="top">
<form method="post" action="">
<table width="100%" cellpadding="0" cellspacing="0" border="0" id="reply">
<tr>
<td><h2>'.$type.'</h2></td>
</tr>
<tr>
<td width="1%" valign="top" nowrap><b>To:</b><br><input name="to" id="focus" title="Enter a single system user here" value="" type="text" size="64"></td>
</tr>
<tr>
<td nowrap><b>Cc:</b><br><input name="cc"" value="" type="text" size="64"></td>
</tr>
<tr>
<td nowrap><b>Subject:</b><br><input name="subject" title="Enter your subject here" value="" type="text" size="64" maxlength="30"></td>
</tr>
<tr>
<td valign="top"><b>Message:</b><br><textarea name="message" title="Enter your message here" rows="5" cols="50" wrap="virtual"></textarea></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><input type="hidden" name="id" value=""><input type="submit" name="send" value="Send"></td>
</tr>
</table>
</form>
</td>
</tr>
</td>
</tr>';
}
EDIT: Example modification of posted code (I haven't added all the different cases or even changed the output really, just showing the concept - all that's here is a check for a type of Reply and a check for the 'to' POST value.)
function compose($type,$contents=null)
{
$toValue = '';
if(isset($_POST['to']))
{
// Might want to actually validate this to prevent XSS, but this is just a basic example
$toValue = $_POST['to'];
}
echo'
<tr>
<td>
<tr>
<td valign="top">
<form method="post" action="">
<table width="100%" cellpadding="0" cellspacing="0" border="0" id="reply">
<tr>
<td><h2>'.$type.'</h2></td>
</tr>';
if($type == "Reply") {
echo'
<tr>
<td width="1%" valign="top" nowrap><b>To:</b><br><input name="to" id="focus" title="Enter a single system user here" value="' . $toValue . '" type="text" size="64"></td>
</tr>
<tr>
<td nowrap><b>Cc:</b><br><input name="cc"" value="" type="text" size="64"></td>
</tr>';
}
echo'
<tr>
<td nowrap><b>Subject:</b><br><input name="subject" title="Enter your subject here" value="" type="text" size="64" maxlength="30"></td>
</tr>
<tr>
<td valign="top"><b>Message:</b><br><textarea name="message" title="Enter your message here" rows="5" cols="50" wrap="virtual"></textarea></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><input type="hidden" name="id" value=""><input type="submit" name="send" value="Send"></td>
</tr>
</table>
</form>
</td>
</tr>
</td>
</tr>';
}
(Original inquiry)
Is this function processing the results of the form, or is it displaying the form in the first place? Your question is a bit unclear about which point in the process you're talking about.