I am making an admin panel for my website.
I have problem editing many values in a database at one time.
My website is https://jaguarz.cf but the admin panel is not visible to common visitors and only i can control it
Warning: count(): Parameter must be an array or an object that implements Countable
I get this error while counting no of id s i want to change
if (isset($_POST['edit_features'])) {
$count=count($_POST['id_edit']);
for($i=0;$i<$count;$i++){
$sql1="UPDATE projects
SET name='" . $_POST['name_edit'][$i] . "',
link='" . $_POST['link_edit'][$i] . "',
rank='" . $_POST['rank_edit'][$i] . "'
WHERE id='" . $_POST['id'][$i] . "'";
$result1=mysqli_query($db,$sql1);
}
}
Here is an example of my form:
<form method="post" action="admin_panel" style="margin:auto;" class="resize form-style-8">
<div class="table-responsive" style="width:99%;">
<table class="table table-bordered">
<thead>
<tr>
<th style="color: rgb(0,0,0);font-size: 20px;">Id</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Name</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Link</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Rank</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="id_edit" value="1" readonly></td>
<td><input type="text" name="name_edit" value="Videos"></td>
<td><input type="text" name="link_edit" value="https://www.youtube.com/channel/UCXqIrdlG-gsfSGuFUt0v-1g"></td>
<td><input type="text" name="rank_edit" value="AAA"></td>
</tr>
<tr>
<td><input type="text" name="id_edit" value="2" readonly></td>
<td><input type="text" name="name_edit" value="Basic Message Encrypter And Decrypter"></td>
<td><input type="text" name="link_edit" value="project?page=en-decrypter"></td>
<td><input type="text" name="rank_edit" value="B"></td>
</tr>
<tr>
<td><input type="text" name="id_edit" value="5" readonly></td>
<td><input type="text" name="name_edit" value="Calculator"></td>
<td><input type="text" name="link_edit" value="project?page=calculator"></td>
<td><input type="text" name="rank_edit" value="E"></td>
</tr>
</tbody>
</table>
<div style="margin: 20px 0px 0px 0px">
<button type="submit" name="edit_features" class="btn btn-info text-justify" style="margin: 10px 0px 10px 0px; width:auto; text-align:center !important;">Edit Projects</button>
</div>
</div><br><br>
<div class="table-responsive" style="width:99%;">
<table class="table table-bordered">
<thead>
<tr>
<th style="color: rgb(0,0,0);font-size: 20px;">Id</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Name</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Link</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Rank</th>
</tr>
</thead>
<tbody>
<tr>
<td>Auto Generated</td>
<td><input type="text" name="name_add"></td>
<td><input type="text" name="link_add"></td>
<td><input type="text" name="rank_add"></td>
</tr>
</tbody>
</table>
<div style="margin: 20px 0px 0px 0px">
<button type="submit" name="add_features" class="btn btn-danger text-justify" style="margin: 10px 0px 10px 0px; width:auto; text-align:center !important;">Add Projects</button>
</div>
</div>
</form>
I expect it to change all values at one time
There are couple of principal issues with your code:
<input type="text" name="id_edit" value="1" disabled>
(as per your original code)
In these inputs
1) disabled elements do not get submitted by the browser. You should change them to readonly (as you've already done in your edit)
2) name="id_edit" is repeated multiple times, but you can't have multiple items with the same name - the server will only see one of them. To make it submit an array of values under the same name, change it to name="id_edit[]". You need to do this for all your repeatable fields.
The last issue is a typo in your server code:
WHERE id='" . $_POST['id'][$i] . "'";
needs to be
WHERE id='" . $_POST['id_edit'][$i] . "'";
because you aren't submitting any field called id in your form.
Lastly I'm going to issue a broader warning, for an issue you need to rectify now you've got the form working on a basic level: Your code is vulnerable to SQL Injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. Never insert unsanitised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data.
you got that warning message because you pass a single value data inside form you need to modified attribute name in each tag input like this
<tr>
<td><input type="text" name="id_edit[]" value="1" readonly></td>
<td><input type="text" name="name_edit[]" value="Videos"></td>
<td><input type="text" name="link_edit[]" value="https://www.youtube.com/channel/UCXqIrdlG-gsfSGuFUt0v-1g"></td>
<td><input type="text" name="rank_edit[]" value="AAA"></td>
</tr>
<tr>
<td><input type="text" name="id_edit[]" value="2" readonly></td>
<td><input type="text" name="name_edit[]" value="Basic Message Encrypter And Decrypter"></td>
<td><input type="text" name="link_edit[]" value="project?page=en-decrypter"></td>
<td><input type="text" name="rank_edit[]" value="B"></td>
</tr>
as above you send an array to php that you can use count for every data has sent from the form and do your stuff on php as well.
but I tell you that way is not the best way.. maybe you can use
data[]['id_edit']
in name attribute as well so you just need to
count($_POST['data'])
without worrying about imbalance count for every data
Related
Hello I have a form like the below image-
The HTML code for this is-
<table style="width:100%" class="table table-striped">
<tr>
<td><strong>First Name</strong></td>
<td>Test Name</td>
<td><a class="btn btn-success" href="#">Edit</a></td>
</tr>
<tr>
<td><strong>Last Name</strong></td>
<td>Test Name</td>
<td><a class="btn btn-success" href="#">Edit</a></td>
</tr>
<tr>
<td><strong>Username</strong></td>
<td>test_username</td>
<td><a class="btn btn-success" href="#">Edit</a></td>
</tr>
</table>
Now what I am trying here is when I click on edit button I only need to be able to edit that specific field and store the updated value to my database only. All I want to know is, each time I click my form submit button only the updated field value should be passed to my controller method. I can pass my field name by a hidden input.
(I know I can keep a hidden input field in a form and change the field value by javascript. But I need to know if there is any better approach.)
Can I implement this using only one form tag in my Laravel blade? If yes how can I implement this without any touch in JS?
Okay, so you can do this with a css trick, look that I have added the input hidden with css, and instead of the a tag I used a label that is linked to the input with the for and id attributes, so when the user clicks on the label it focus on the input and triggers the css input:focus that shows the input and hide the name.
All is wrapped by a single form that if you add a <button type="input">Send</button> will send all to the controller
But you've mentioned that you just want to send the data that has been changed, unfortunately I don't think that this is possible without javascript, but you should set the value of the input to the original value, so when updated it will update to the same value, no problem at all, acctually almost all edit forms works that way
input {
opacity: 0;
width: 0;
}
input:focus {
visibility: visible;
opacity: 1;
width: 100px;
}
input:focus + span {
display: none;
}
<form>
<table style="width:100%" class="table table-striped">
<tr>
<td><strong>First Name</strong></td>
<td>
<input id="first_name" name="first_name" value="Test Name" />
<span>Test Name</span>
</td>
<td>
<label for="first_name" class="btn btn-success" href="#">Edit</label>
</td>
</tr>
<tr>
<td><strong>Last Name</strong></td>
<td>
<input id="last_name" value="Test Name" />
<span>Test Name</span>
</td>
<td>
<label for="last_name" class="btn btn-success" href="#">Edit</label>
</td>
</tr>
<tr>
<td><strong>Last Name</strong></td>
<td>
<input id="username" value="test_username" />
<span>test_username</span>
</td>
<td>
<label for="username" class="btn btn-success" href="#">Edit</label>
</td>
</tr>
</table>
</form>
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.
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.
hi i have an php form which contains many fields, now i tried to write the code for storing all those values into an array and store them in a cookie in javascript. the code is as follows :
<script type="text/javascript">
function setDate()
{
var today = new Date();
var month = today.getMonth() + 1;
var day = today.getDate();
var year = today.getFullYear();
document.forms['form1'].elements['date'].value= month + "/" + day + "/" + year;
}
function add_newoption(optionname)
{
document.getElementById(optionname).InnerHTML = "";
}
var win = null;
function optionWindow(mypage,myname,w,h){
LeftPosition = (screen.width) ? (screen.width-w)/2: 0;
TopPosition = (screen.height) ? (screen.height-h)/2: 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition;
win = window.open(mypage,myname,settings);
win.focus();
}
function setAction(act)
{
document.forms['form1'].action = act;
document.forms['form1'].submit();
}
</script>
and the code for the form is as follows:
<body onLoad="setDate();">
<br/>
<center>
<fieldset class="style1" style="width:800px; background-color:#FFFFFF">
<h4 align="center"> Designer Form </h4>
<form id="form1" name="form1" method="post" >
<table width="700" border="0" cellpadding="2" cellspacing="2" bgcolor="#FFFFFF" style="text-align:left; padding: 25px 0px 25px 0px;">
<tr>
<td width="174" align="right">Date:</td>
<td width="331"><input name="date" type="text" size="10" readonly/></td>
<td width="175" style="text-align:center;">Order Number:
<input name="order_num" type="text" size="10" value=""/></td>
</tr>
<tr>
<td > </td>
<td> </td>
<td style="text-align:center;"><input name="orderdetails" type="submit" value="Get Order Details" onClick="setAction('?action=getorder');"/></td>
</tr>
<tr>
<td colspan='4'> <input name="email" id="email" type="hidden" value=""/> </td>
</tr>
<tr>
<td align="right">First Name:</td>
<td><input name="fname" type="text" value=""/></td>
<td> </td>
</tr>
<tr>
<td align="right">Last Name:</td>
<td><input name="lname" type="text" value=""/></td>
<td> </td>
</tr>
<tr>
<td align="right">Image Submitted:</td>
<td><input name="image" type="text" /></td>
<td> </td>
</tr>
<tr>
<td align="right">General Comments: </td>
<td><textarea name="gen_comments" cols="50" rows="6"></textarea></td>
<td> </td>
</tr>
<tr>
<td align="right">Internal Comments:</td>
<td><textarea name="int_comments" cols="50" rows="6"></textarea></td>
<td> </td>
</tr>
<tr>
<td align="right">Quality of the File:</td>
<td colspan="2" style="padding-right:4px">
<select name="quality">
<option>Select One</option><option value="good">Good</option><option value="ok">A bit low but we can use it</option><option value="low">Low. We are concerned it might effect the qaulity of the final</option><option value="not good">Not good. We cannot work with it</option><option value="test2">test2</option><option value="test">test</option><option value="lisa">Lisa1</option><option value="bbb">aaa</option> <!--option value="good">Good</option>
<option value="ok">A bit low but we can use it</option>
<option value="low">Low. We are concerned it might effect the qaulity of the final </option>
<option value="not good">Not good. We can't work with it</option-->
</select>
now what i want to do here is to get all these values entered in the form to store into an array and have to set cookie to retrieve them when the page re-loads...
how can i write a function for that using javascript and where to call that function in the form for getting those values into the form on page reloads..??
Any help will be needful to me....thanks in advance..
well i think you should use localStorage.setItem("DataKey","DataValue"); for store any value.
and for getting value you can use localStorage.getItem("DataKey");
FF,chrome,safari are support localStorage but i dont know about IE.
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.