How would I display student.Student instead of student_id
<?php
for ($i = 0; $i < $chkcount; $i++) {
$id = $chk[$i];
$res = $MySQLiconn->query("SELECT Student.ID, student.Student, student.School,student.PR, Jumps.dis_ft_1, Jumps.dis_in_1, Jumps.dis_ft_2, Jumps.dis_in_2, Jumps.dis_ft_3, Jumps.dis_in_3, Jumps.dis_ft_4, Jumps.dis_in_4, Jumps.dis_ft_5, Jumps.dis_in_5, Jumps.dis_ft_6, Jumps.dis_in_6
FROM Student LEFT JOIN Jumps ON Student.ID = Jumps.student_id WHERE ID=" . $id);
while ($row = $res->fetch_array()) {
?>
<tr>
<td>
<input type="hidden" name="id[]" value="<?php echo $row['ID']; ?>"/>
Student ID: <input type="text" name="st[]" value="<?php echo $row['student_id']; ?>"
class="form-control"/>
Attempt 1 <input type="text" name="df1[]" value="<?php echo $row['dis_ft_1']; ?>"
class="form-control"/>
<input type="text" name="di1[]" value="<?php echo $row['dis_in_1']; ?>"
class="form-control"/>
Attempt 2 <input type="text" name="df2[]" value="<?php echo $row['dis_ft_2']; ?>"
class="form-control"/>
<input type="text" name="di2[]" value="<?php echo $row['dis_in_2']; ?>"
class="form-control"/>
Attemp 3 <input type="text" name="df3[]" value="<?php echo $row['dis_ft_3']; ?>"
class="form-control"/>
<input type="text" name="di3[]" value="<?php echo $row['dis_in_3']; ?>"
class="form-control"/>
Attempt 4 <input type="text" name="df4[]" value="<?php echo $row['dis_ft_4']; ?>"
class="form-control"/>
<input type="text" name="di4[]" value="<?php echo $row['dis_in_4']; ?>"
class="form-control"/>
Attempt 5 <input type="text" name="df5[]" value="<?php echo $row['dis_ft_5']; ?>"
class="form-control"/>
<input type="text" name="di5[]" value="<?php echo $row['dis_in_5']; ?>"
class="form-control"/>
Attempt 6 <input type="text" name="df6[]" value="<?php echo $row['dis_ft_6']; ?>"
class="form-control"/>
<input type="text" name="di6[]" value="<?php echo $row['dis_in_6']; ?>"
class="form-control"/>
</td>
</tr>
<?php
}
}
?>
Not sure it can be done. But Student name would be better than student id.
Wouldn't post with more text so just typing so it will post.
I am learning PHP.
If there is only one field called Student i.e Student.Student, then you could just output the name <?php echo $row['Student']; ?>.
However, if there are ever name clashes, you can always give the field a new name for PHP to access.
SELECT Student.ID, Student.Student AS some_new_name FROM ....
<?php echo $row['some_new_name']; ?>
Related
I am trying to fill a html form with data being received out of my mysql database. However I cannot set the forms to display on-load the variables being extracted from the database. I would like the form on-load to hold the data last entered into the forms which have been added to the database previously.
$query = "SELECT FROM character_tbl WHERE character_player
='".$_SESSION["user"]."' character_tbl";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$name = $row['character_name'];
$race = $row['character_race'];
$class = $row['character_class'];
$alignment = $row['character_alignment'];
$hp = $row['character_hp'];
$str = $row['character_str'];
$dex = $row['character_dex'];
$con = $row['character_con'];
$int = $row['character_int'];
$wis = $row['character_wis'];
$cha = $row['character_cha'];
$ac = $row['character_ac'];
$touch = $row['character_touch'];
$flat = $row['character_flat'];
$fort = $row['character_fort'];
$ref = $row['character_ref'];
$will = $row['character_will'];
}
echo $will;
mysql_close();
?>
<!DOCTYPE html>
<html>
<body>
<div id="nav">
<form action="user.php">
<input type="submit" value="Back">
</form>
</div>
<div id="section">
<form action="update.php" method="POST">
Character Name:<br>
<input type="text" name="name" value="<?php echo $name;?>">
<br>
Race<br>
<input type="text" name="race" value="<?php echo $race;?>">
<br>
Class<br>
<input type="text" name="class" value="<?php echo $class;?>">
<br>
Alignment<br>
<input type="text" name="alignment" value="<?php echo $alignment;?>">
<br>
HP<br>
<input type="text" name="hp" value="<?php echo $hp;?>">
<br>
STR<br>
<input type="number" name="str" value="<?php echo $str;?>">
<br>
DEX<br>
<input type="number" name="dex" value="<?php echo $dex;?>">
<br>
CON<br>
<input type="text" name="con" value="<?php echo $con;?>">
<br>
INT<br>
<input type="text" name="int" value="<?php echo $int;?>">
<br>
WIS<br>
<input type="text" name="wis" value="<?php echo $wis;?>">
<br>
CHA<br>
<input type="text" name="cha" value="<?php echo $cha;?>">
<br>
AC<br>
<input type="text" name="ac" value="<?php echo $ac;?>">
<br>
Touch AC<br>
<input type="text" name="touch" value="<?php echo $touch;?>">
<br>
Flat-Footed AC<br>
<input type="text" name="flat" value="<?php echo $flat;?>">
<br>
Fortitude<br>
<input type="text" name="fort" value="<?php echo $fort;?>">
<br>
Reflex<br>
<input type="text" name="ref" value="<?php echo $ref;?>">
<br>
Will<br>
<input type="text" name="will" value="<?php echo $will;?>">
</br>
<input type="submit" value="Update">
</form>
I think the SQL has error:
SELECT FROM character_tbl WHERE character_player
try:
SELECT * FROM character_tbl WHERE character_player
You have syntax error in your mysql query. You have not place field or columns name or (*) for all columns to extract.
Try like this..
$query = "SELECT * FROM character_tbl WHERE character_player ='".$_SESSION['user']."'";
Trying to get an edit page to work and im getting this error Incorrect parameter count in the call to native function 'SHA1'. The error is only on the SHA1. everything else works fine other then this error. Any helped is greatly appreciated. I don't want the password showing or the encryption that is to stay blank as it is an admin editing a staff members details and they dont need to know the password.
<?php
if(empty($_POST['submit'])) {
$query = "SELECT * FROM user WHERE user_id = '$_GET[id]'";
// $supplier_id = $_GET['supplier_id'];
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
}
else{
$update = "UPDATE user SET
title= '$_POST[inputtitle]',
first_name ='$_POST[inputfirst_name]',
middle_name = '$_POST[inputmiddle_name]',
last_name = '$_POST[inputlast_name]',
gender = '$_POST[inputgender]',
email = '$_POST[inputemail]',
pass = SHA1($_POST[inputSHA1pass]);
car_make = '$_POST[inputcar_make]'
car_model = '$_POST[inputcar_model]',
prefer_car = '$_POST[inputprefer_car]',
car_age = '$_POST[inputcar_age]',
st_no = '$_POST[inputst_no]',
st_name = '$_POST[inputst_name]',
suburb = '$_POST[inputsuburb]',
state = '$_POST[inputstate]',
country = '$_POST[inputcountry]',
postcode = '$_POST[inputpostcode]',
WHERE user_id = $_POST[user_id]";
mysqli_query($dbc, $update) or die(mysqli_error($dbc));
echo "User has been modified!";
header("Location: view_user.php");
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'].htmlspecialchars($_GET[‘id’]); ?>" method="POST">
<input type="hidden" value="<?php echo $supplier = $_GET['id']; ?>" name="supplier_id">
<p>Title:
<input type="text" name="inputtitle" size="20" maxlength="30"
value="<?php echo $row["title"]; ?>"/>
</p>
<p>First Name:
<input type="text" name="inputfirst_name" size="20" maxlength="30"
value="<?php echo $row["first_name"]; ?>"/></p>
<p>Middle Name:
<input type="text" name="inputmiddle_name" size="30" maxlength="100"
value="<?php echo $row["middle_name"]; ?>"/></p>
<p>Last Name:
<input type="text" name="inputlast_name" size="20" maxlength="30"
value="<?php echo $row["last_name"]; ?>"/></p>
<p>Gender:
<input type="text" name="inputgender" size="20" maxlength="30"
value="<?php echo $row["gender"]; ?>"/></p>
<p>Email:
<input type="text" name="inputemail" size="10" maxlength="5"
value="<?php echo $row["email"]; ?>"/>
</p><br>
<p>Password:
<input type="text" name="inputSHA1pass" size="30" maxlength="40"
value="<?php if (isset($_POST['pass'])) echo $_POST['pass']; ?>"/></p>
<p>Car Make:
<input type="text" name="inputcar_make" size="20" maxlength="30"
value="<?php echo $row["car_make"]; ?>"/></p>
<p>Car Model:
<input type="text" name="inputcar_model" size="20" maxlength="30"
value="<?php echo $row["car_model"]; ?>"/></p>
<p>Prefered Car:
<input type="text" name="inputprefer_car" size="20" maxlength="30"
value="<?php echo $row["prefer_car"]; ?>"/></p>
<p>Car Age:
<input type="text" name="inputcar_age" size="20" maxlength="30"
value="<?php echo $row["car_age"]; ?>"/></p>
<p>Street Number:
<input type="text" name="inputst_no" size="20" maxlength="30"
value="<?php echo $row["st_no"]; ?>"/></p>
<p>Street Name:
<input type="text" name="inputst_name" size="20" maxlength="30"
value="<?php echo $row["st_name"]; ?>"/></p>
<p>Suburb:
<input type="text" name="inputsuburb" size="20" maxlength="30"
value="<?php echo $row["suburb"]; ?>"/></p>
<p>State:
<input type="text" name="inputstate" size="20" maxlength="30"
value="<?php echo $row["state"]; ?>"/></p>
<p>Country:
<input type="text" name="inputcountry" size="20" maxlength="30"
value="<?php echo $row["country"]; ?>"/></p>
<p>Postcode:
<input type="text" name="inputpostcode" size="20" maxlength="30"
value="<?php echo $row["postcode"]; ?>"/></p>
<p>
<input type="submit" name="submit" value="Submit"/></p>
</form>
<?php
mysqli_close($dbc);
?>
<br>
<br>
</div>
<br>
<?php
include ('../includes/footer.php');
?>
A few things:
Check that $_POST['inputSHA1pass'] is not empty
Enclose $_POST[inputSHA1pass] in quote marks and curly braces: SHA1('{$_POST[inputSHA1pass]}'). SHA1() expects a string and will error without one
Look into prepared statements. As this code stands, it is wide open to SQL injection attacks as none of your passed data is checked for validity
Change the semi-colon to a comma in the SHA1 line
Finally, once the UPDATE query has been built, capture it and try running it directly on the database server. It should give you a more meaningful error message and make debugging easier
I have a site where the user registers and logs in and then is taken to a page to enter data.
I have successfully been able to recall all the fields in the database as input values into my form and I want to be able to update these values with a save button before submitting.
I have two form buttons providing different actions based on javascript.
I don't receive any errors but when I hit the save button no entries submit to my database.
I am pulling the unique key (email address) from the database and am trying to update all of the fields associated with the row the unique email address is in. I have this field as a hidden input that gets added to the submission when the save button is clicked.
Ive tried REPLACE, ON DUPLICATE KEY UPDATE to update the entry but it still does nothing. Here is my code:
<div style="float:left; width:450px;">
<form id="parts" class="form" name="parts" method="post" ><br>
<input type="hidden" id="email" name="email" value="<? echo $_SESSION['MM_Username']; ?>">
<input type="hidden" id="first_name" name="first_name" value="<? echo $user['first_name']; ?>">
<input type="hidden" id="last_name" name="last_name" value="<? echo $user['last_name']; ?>">
<input type="hidden" id="business_name" name="business_name" value="<? echo $user['business_name']; ?>">
<input type="hidden" id="occupation" name="occupation" value="<? echo $user['occupation']; ?>">
<input type="hidden" id="business_address_1" name="business_address_1" value="<? echo $user['business_address_1']; ?>">
<input type="hidden" id="business_address_2" name="business_address_2" value="<? echo $user['business_address_2']; ?>">
<input type="hidden" id="country" name="country" value="<? echo $user['country']; ?>">
<input type="hidden" id="city" name="city" value="<? echo $user['city']; ?>">
<input type="hidden" id="state_province" name="state_province" value="<? echo $user['state_province']; ?>">
<input type="hidden" id="zip" name="zip" value="<? echo $user['zip']; ?>">
<input type="hidden" id="phone" name="phone" value="<? echo $user['phone']; ?>">
<input type="hidden" id="special_offers" name="special_offers" value="<? echo $user['special_offers']; ?>">
<div id="div1"> </div>
<div id="div2">SET PART #</div>
<div id="div3">INSTALL DATE</div>
<div id="div1">1.</div>
<div id="div2"><input type="text" id="part_1" name="part_1" value="<? echo $user['part_1']; ?>"> </div>
<div id="div3"><input type="text" id="part_1_install_date" name="part_1_install_date" value="<? echo $user['part_1_install_date']; ?>"> </div>
<div id="div1">2.</div>
<div id="div2"><input type="text" id="part_2" name="part_2" value="<? echo $user['part_2']; ?>"></div>
<div id="div3"><input type="text" id="part_2_install_date" name="part_2_install_date" value="<? echo $user['part_2_install_date']; ?>"></div>
<div id="div1">3.</div>
<div id="div2"><input type="text" id="part_3" name="part_3" value="<? echo $user['part_3']; ?>"></div>
<div id="div3"><input type="text" id="part_3_install_date" name="part_3_install_date" value="<? echo $user['part_3_install_date']; ?>"></div>
<div id="div1">4.</div>
<div id="div2"><input type="text" id="part_4" name="part_4" value="<? echo $user['part_4']; ?>"></div>
<div id="div3"><input type="text" id="part_4_install_date" name="part_4_install_date" value="<? echo $user['part_4_install_date']; ?>"></div>
<div id="div1">5.</div>
<div id="div2"><input type="text" id="part_5" name="part_5" value="<? echo $user['part_5']; ?>"></div>
<div id="div3"><input type="text" id="part_5_install_date" name="part_5_install_date" value="<? echo $user['part_5_install_date']; ?>"></div>
<div id="div1">6.</div>
<div id="div2"><input type="text" id="part_6" name="part_6" value="<? echo $user['part_6']; ?>"></div>
<div id="div3"><input type="text" id="part_6_install_date" name="part_6_install_date" value="<? echo $user['part_6_install_date']; ?>"></div>
<div id="div1">7.</div>
<div id="div2"><input type="text" id="part_7" name="part_7" value="<? echo $user['part_7']; ?>"></div>
<div id="div3"><input type="text" id="part_7_install_date" name="part_7_install_date" value="<? echo $user['part_7_install_date']; ?>"></div>
<div id="div1">8.</div>
<div id="div2"><input type="text" id="part_8" name="part_8" value="<? echo $user['part_8']; ?>"></div>
<div id="div3"><input type="text" id="part_8_install_date" name="part_8_install_date" value="<? echo $user['part_8_install_date']; ?>"></div>
<div id="div1">9.</div>
<div id="div2"><input type="text" id="part_9" name="part_9" value="<? echo $user['part_9']; ?>"></div>
<div id="div3"><input type="text" id="part_9_install_date" name="part_9_install_date" value="<? echo $user['part_9_install_date']; ?>"></div>
<div id="div1">10.</div>
<div id="div2"><input type="text" id="part_10" name="part_10" value="<? echo $user['part_10']; ?>"></div>
<div id="div3"><input type="text" id="part_10_install_date" name="part_10_install_date" value="<? echo $user['part_10_install_date']; ?>"></div>
<div id="div1"> </div>
<div id="div2"><input type="submit" name="save" value="Save" onclick="submitForm('save.php')"></div>
<div id="div3"><input class="send" id="register" type="submit" value="Submit" onclick="submitForm('submit.php')" disabled="disabled"></div>
</form>
LOGOUT
</div>
Here is my save button onclick action:
<?php
include("includes/connection.php");
$sql = "INSERT INTO login (email, first_name, last_name, business_name, occupation, business_address_1, business_address_2, country, city, state_province, zip, phone, special_offers, part_1, part_1_install_date, part_2, part_2_install_date, part_3, part_3_install_date, part_4, part_4_install_date, part_5, part_5_install_date, part_6, part_6_install_date, part_7, part_7_install_date, part_8, part_8_install_date, part_9, part_9_install_date, part_10, part_10_install_date)
VALUES ('$_POST[email]','$_POST[first_name]','$_POST[last_name]','$_POST[business_name]','$_POST[occupation]','$_POST[business_address_1]','$_POST[business_address_2]','$_POST[country]','$_POST[city]','$_POST[state_province]','$_POST[zip]','$_POST[phone]','$_POST[special_offers]','$_POST[part_1]', '$_POST[part_1_install_date]', '$_POST[part_2]', '$_POST[part_2_install_date]', '$_POST[part_3]', '$_POST[part_3_install_date]', '$_POST[part_4]', '$_POST[part_4_install_date]', '$_POST[part_5]', '$_POST[part_5_install_date]', '$_POST[part_6]', '$_POST[part_6_install_date]', '$_POST[part_7]', '$_POST[part_7_install_date]', '$_POST[part_8]', '$_POST[part_8_install_date]', '$_POST[part_9]', '$_POST[part_9_install_date]', '$_POST[part_10]', '$_POST[part_10_install_date]')
ON DUPLICATE KEY UPDATE login SET column = 'email' WHERE column='$_POST[email]'";
if (mysqli_query($connection, $sql)) {
echo header('Location: login-ENG.php');
} else {
echo mysqli_error($connection);
}
mysqli_close($connection);
?>
Here is the javascript to differentiate button functions:
<script>
function submitForm(action)
{
document.getElementById('parts').action = action;
document.getElementById('parts').submit();
}
</script>
NONE of your form fields have name attributes. Without name=..., there's nothing to submit. id attributes are not relevant at all for form submission. A simple var_dump($_POST) would have shown you this.
<input type="text" name="email" ... />
^^^^^^^^^^^^
|
VALUES ('$_POST[email]','$
As well, you are vulnerable to sql injection attacks
i want insert post text box values of each selected check boxes to the database.
<input type="checkbox" name="email[]" value="<?php echo $actions_to_be[$z]['student_email'] ?>" />
<input type="text" name="timetable_id[]" value="<?php echo $actions_to_be[$z]['timetable_id'] ?>" size="10px"/>
<input type="text" name="sid[]" value="<?php echo $actions_to_be[$z]['sid'] ?>" size="10px" />
<input type="checkbox" name="email[]" value="<?php echo $i; ?>" />
<input type="text" name="timetable_id[<?php echo $i; ?>]" value="<?php echo $actions_to_be[$z]['timetable_id'] ?>" size="10px"/>
<input type="text" name="sid[<?php echo $i; ?>]" value="<?php echo $actions_to_be[$z]['sid'] ?>" size="10px" />
$i should be increased for every box of course. Then you can simply take the ids from the $_POST['email'] array (possibly rename it to something more meaningful though) and retrieve the values in $_POST['timetable_id'][$id] and $_POST['sid'][$id].
I have a big form that contains X amount of posts that has 15 fields per post along with 1 hidden field.
Let's assume I have 14 posts. This means my form would send 211 fields (14x15 fields plus 1 hidden field).
The user does not have to fill in all fields.
I want to count the number of posts that the form sends but I seem to be running into difficulty.
Using count($_POST) returns 152. This leads me to believe that count() is ignoring empty fields.
As a result, using a formula such as (count($_POST) - 1) / 15 would return the wrong result (10.0666) and is inefficient should the number of fields change in the future.
So, does anyone have any ideas as to how to get the proper count of my posts?
My form looks like so:
<form name="scraped" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" method="post">
<input type="hidden" name="OSscraper_hidden" value="N">
<?php
$inpCnt = 0;
foreach($articles as $item) {
?>
<input type="text" name="title_<?php echo $inpCnt; ?>">
<input type="text" name="name_<?php echo $inpCnt; ?>">
<input type="text" name="url_<?php echo $inpCnt; ?>">
<input type="text" name="img_<?php echo $inpCnt; ?>">
<input type="text" name="pet_<?php echo $inpCnt; ?>">
<input type="text" name="color_<?php echo $inpCnt; ?>">
<input type="text" name="value_<?php echo $inpCnt; ?>">
<input type="text" name="height_<?php echo $inpCnt; ?>">
<input type="text" name="weight_<?php echo $inpCnt; ?>">
<input type="text" name="hair_<?php echo $inpCnt; ?>">
<input type="text" name="eyes_<?php echo $inpCnt; ?>">
<input type="text" name="race_<?php echo $inpCnt; ?>">
<input type="text" name="phone_<?php echo $inpCnt; ?>">
<input type="text" name="address_<?php echo $inpCnt; ?>">
<input type="text" name="zip_<?php echo $inpCnt; ?>">
<?php
$inpCnt++;
} ?>
<input type="submit" value="Submit">
</form>
Change your form to look like:
<input type="text" name="foo[<?php echo $inpCnt; ?>][title]">
<input type="text" name="foo[<?php echo $inpCnt; ?>][name]">
<input type="text" name="foo[<?php echo $inpCnt; ?>][url]">
Then you will get:
$_POST['foo'] = [
0 => ['title' => '...', 'name' => '...', 'url' => '...'],
1 => ...,
...
];
It saves you from having to do the grouping yourself, and is easier to count or iterate over the input.
why not just count($articles)*15 and echo into a hidden input. You are using another hidden input anyway....
Try this code, and demo is here
Please just use the idea not the exact copy.
<?php
error_reporting(E_ALL ^ E_NOTICE);
//debugging
if(#$_POST['submit'] == 'Submit'){
echo '<pre>';
print_r($_POST);
echo '</pre>';
echo "<br>\n";
echo 'Number of posts = count($_POST["posts"])='.count(#$_POST['posts'])."<br>\n";
//finding number of posts that are set and not empty
$count = 0;
foreach($_POST['posts'] as $v1){
//$v is an array
foreach($v1 as $v1k=> $v1v){
if(strlen($v1v) > 0){
++$count;
$inputs[$v1k] = $v1v;
}
}
}
echo 'Count of non-empty posts = $count = '.$count."<br>\n";
echo '<pre>';
print_r($inputs);
echo '</pre>';
}
?>
<form name="scraped" action="" method="post">
<input type="hidden" name="OSscraper_hidden" value="N">
<?php
$articles =array('test');
$inpCnt = 0;
foreach($articles as $item) {
?>
<input type="text" name="posts[][title_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][name_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][url_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][img_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][pet_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][color_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][value_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][height_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][weight_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][hair_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][eyes_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][race_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][phone_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][address_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][zip_<?php echo $inpCnt; ?>]">
<?php
$inpCnt++;
} ?>
<input type="submit" value="Submit" name="submit">
</form>