I'm trying to get dns records of a domain through dns_get_record() but the function doesn't seem to work when a variable is inserted..here's my code
<form action="" method="post">
<input type="text" name="host" placeholder="Enter IP or Domain"/>
<select name="dns">
<option value="DNS_A" selected="selected">A</option>
....
<option value="DNS_ANY">ANY</option>
</select>
</form>
<?php
$host=$_POST['host'];
$dns=$_POST['dns'];
$type=end(explode('_',$dns));
if ($host==""){
exit();
}
echo "Results for $host $type record<br />";
$result = dns_get_record($host, $dns);
echo "Result = ";
print_r($result);
?>
but if i put
$result = dns_get_record($host, DNS_A);
instead of
$result = dns_get_record($host, $dns);
it works..help!
DNS_A is a constant of value 1, while you are passing string "DNS_A", try by passing value of constant, using $result = dns_get_record($host, constant($dns));
This is because your POST returns a literal string: "DNS_A", if you prefer. This is NOT the same as DNS_A, which is a constant and most likely contains an integer.
You'll need a mapping table for this one. Or just pass the integer value straight off.
Related
Perhaps there may be an easier way to do this however, I need the project to select a patient from the drop down menu. Then when the dropdown menu has got a value, the text field needs to take the NHS number from that drop down menu (array) so that it can be posted elsewhere.
<select name="patient" class="textbox" required>
<option value="">Select a patient</option>
<?php
include 'dbconnection.php';
$sql = "SELECT * FROM clients ORDER by firstname ASC";
$result = mysqli_query($conn, $sql);
$result = $conn-> query($sql);
while($row=mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row["firstname"]." ".$row["lastname"]; ?>">
<?php echo $row["firstname"]." ".$row["lastname"] .", ".$row["addressl1"].", ".$row["addressl2"].", ".$row["county"].", ".$row["postcode"].", ".$row["nhsnum"]; ?></option>
<?php
$nhs = $row['nhsnum'];
}
?>
</select>
<?php
$sql = "SELECT * FROM clients WHERE nhsnum = $nhs ";
$result = mysqli_query($conn, $sql);
$result = $conn-> query($sql);
while($row=mysqli_fetch_array($result))
{
?>
<input type="text" placeholder="NHS number" readonly value=" <?php echo $row["nhsnum"]; ?>">
<?php
}
?>
As you may see, I have created dummy variables of $nhs however its a static variable and doesnt change upon user selection from the drop down list. Can anyone explain how I can merge the two together.
DB setup
i think you should declare the $nhs outside the while loop
Use AJAX, as already suggested, or a form submit button. Your second query should be where your AJAX or submitted form goes. Use $_GET or $_POST, if you are using get or post method, to intercept the option value. Assign that value to your $nhs, then use as you have.
Set the option value to the $nhs value you want, not the person’s name. Example using $_POST
if(isset($_POST['patient'])){
$nhs=$_POST['patient'];
}else{
// whatever code you want to handle a non-submitted value.
}
Add additional code to prevent SQL injection.
I am trying to make a dropdown list with values from a database, but when I try testing whether the value was taken in, I get nothing.
<select name = "student_id">
<option value="" >choose student</option>
<?php
require_once('../mysqli_connect.php');
$getnames = #mysqli_query($dbc, "SELECT student_id, first_name, last_name FROM students");
while ($row = mysqli_fetch_array($getnames))
{
?><option value = "<?php echo $row['student_id']; ?>">
<?php echo $row['student_id'] . ' ' .$row['first_name'] . ' '. $row['last_name'];
?></option>"; <?php
}
?>
</option>
</select>
The list appears to be fine, showing the student id, first name and the last name of all entries in the database, but when I test the value taken in with this
$studentid = trim($_POST['student_id']);
echo $studentid;
require_once ('../mysqli_connect.php');
?>
And it does not return anything. Everything seems to work with other lists where I did not use any loops and just typed out each option, but I can't find issues In the loop.
Sorry for the terrible question.
?><option value = "<?php $row['student_id']; ?>">
^---missing echo
If you'd done any basic debugging, like a view source of your generated html, you'd have seen that all of your option values are empty...
So, not, it's not the option refusing to store a value. it's doing EXACTLY what YOU told it to: send an empty/blank value to the server.
I have gone through various posts but can't find a solution to this problem:
I have a form with several rows of fields to insert in a database table with one Click of the submit button. This is the code of the HTML:
<form action="filename.php?cartID=<?php echo $_GET['cartID'];?>&customer_id=<?php echo $_GET['customer_id'];?>&total_count=<?php echo $_GET['total_count'];?>&action=add" method="post" id="add_participants" >
<table>
<?php for ($i=0, $n=$_GET['total_count']; $i<$n; $i++) { ?>
<input type="hidden" name="customer_id[]" id="customer_id[]" value="<?php echo $_GET['customer_id'];?>" />
<input type="hidden" name="cartID[]" id="cartID[]" value="<?php echo $_GET['cartID'];?>" />
<input type="hidden" name="products_id[]" id="products_id[]" value="<?php echo $_GET['products_id'];?>" />
<tr><td><label for="title[]">Title</label></td><td><select id="title[]" name="title[]">
<option value="Dr">Dr</option>
<option value="Miss">Miss</option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Prof">Prof</option>
</select>
</td>
<td><label for="firstname[]">First Name</label></td><td><input type="text" id="firstname[]" name="firstname[]"/></td>
<td><label for="surname[]">Surname</label></td><td><input type="text" id="surnam[]e" name="surname[]"/></td>
<td><label for="email[]">E-mail</label></td><td><input type="text" id="email[]" name="email[]"/></td></tr>
</table>
<?php } ?>
<input value="Add participant" type="submit" />
On the action page the code is the following:
for ($i=0, $n=$_GET['total_count']; $i<$n; $i++) {
$title[$i] = tep_db_prepare_input($HTTP_POST_VARS['title.$i]']);
$firstname =tep_db_prepare_input($HTTP_POST_VARS['firstname.$i']);
$surname =tep_db_prepare_input($HTTP_POST_VARS['surname.$i']);
$email = tep_db_prepare_input($HTTP_POST_VARS['email.$i']);
$customer_id = tep_db_prepare_input($HTTP_POST_VARS['customer_id.$i']);
$cart_id = tep_db_prepare_input($HTTP_POST_VARS['cartID.$i']);
$products_id = tep_db_prepare_input($HTTP_POST_VARS['products_id.$i']);
$query = "INSERT INTO participants (title,Firstname,Surname,Email,customers_id,cart_id,products_id) VALUES ('$title[$i]', '$firstname[$i]', '$surname[$i]', '$email[$i]', $customers_id[$i], $cart_id[$i], $products[$i])";
echo $query . "<br />";
mysql_query($query) or die(mysql_error());
}
However, I cannot get the values of the $_POST variables into the variable arrays to use in the insert statement.
Can anyone help me with this please? I have tried different permutations of the code, and I'm still not getting anywhere.
Many thanks.
I'm fairly certain that $HTTP_POST_VARS is deprecated.. you should be using $_POST instead.
Secondly, your processing doesn't really need to loop over a for loop. You could use foreach to loop over all posted values instead.
Example (untested):
foreach($_POST['customer_id'] as $key => $customerID) {
$title = !empty($_POST['title'][$key]) ? $_POST['title'][$key] : "";
$firstName = !empty($_POST['firstname'][$key]) ? $_POST['firstname'][$key] : "";
// etc...
}
if your keeping the loop the same way, you need to make a change to how you refer to your array elements. change $title[$i] = tep_db_prepare_input($HTTP_POST_VARS['title.$i]']); to
$title[$i] = tep_db_prepare_input($_POST['title'][$i]);
and change the rest of your assignments to follow the same pattern.
The output of tep_db_prepare_input() is of course a mistery, but apart from that it's true what Marc B. says: "Learn basic PHP syntax rules"
$query = "INSERT INTO participants (title,Firstname,Surname,Email,customers_id,cart_id,products_id) VALUES ('$title[$i]', '$firstname[$i]', '$surname[$i]', '$email[$i]', $customers_id[$i], $cart_id[$i], $products[$i])";
echo $query . "";
This will output exactly what's between the double quotes. Try using:
$query = "INSERT INTO participants (title,Firstname,Surname,Email,customers_id,cart_id,products_id) VALUES ('".$title[$i]."', '".$firstname[$i]."', '".$surname[$i]."', '".$email[$i]."', ".$customers_id[$i].", ".$cart_id[$i].", ".$products[$i].")";
echo $query . "";
Using "{$array[$key]}" within double quotes also works, but using "$array[$key]" within double quotes does not work.
Further, $HTTP_POST_VARS is deprecated. Use $_POST
Your code has multiple errors that result in unintended values:
$title[$i] = tep_db_prepare_input($HTTP_POST_VARS['title.$i]'])
foreach is definitely clearer than a for loop
What others said: $HTTP_POST_VARS must be $_POST (or $_REQUEST)
Assuming that your mysterious tep_db_prepare_input() functions correctly, $HTTP_POST_VARS['title.$i]'] is syntactically incorrect. Single quotes mean variables are not parsed: your function is passed the (invalid) contents of $_POST['title.$i]']. I believe you meant to write $POST["title$i"] (where the "." is part of the field name. (Personally an underscore would be less confusing as "." has meaning in PHP.)
So... fix all that and you should be good to go. Well, #2 & 3, at least.
I'm trying to insert multiple values in the database using select list. What I got so far:
HTML
<form enctype="multipart/form-data" action="" method="post">
<select name="cars[]" multiple="multiple" style="width:300px">
<?php
$getcars = mysql_query("SELECT cars_id, cars_name FROM car");
while ($row = mysql_fetch_assoc($getcars)) {
$car_id = $row['cars_id'];
$car_name = $row['cars_name'];
?>
<option value="<?php echo $car_id ?>"><?php echo $car_name ?></option>
<?php } ?>
</select><br />
<input type="submit" name="submit" value="Submit"/><br/>
</form>
PHP
$cars= $_POST['cars'];
echo $cars;
for($i = 0; $i < count($cars); $i++){
echo $cars[$i];
$carGroups = mysql_query("INSERT INTO car_groups VALUES('$company','$cars[$i]]')");
}
Unfortunately it doesn't work, I tried to print $cars to check the resulted value. It prints "Array", and when I tried to print $cars[$i] it prints nothing.
Does anyone know what the problem is?
There is an extra closing bracket that should be removed. You are not checking if your query was successful or not.
$carGroups = mysql_query("INSERT INTO car_groups VALUES('$company','$cars[$i]]')");
should be:
$carGroups = mysql_query("INSERT INTO car_groups VALUES('$company','$cars[$i]')") or die(mysql_error());
Since $cars is an array, you can print its content using print_r or var_dump:
print_r($cars);
var_dump($cars);
Useful reading:
How to get useful error messages in PHP?
mysql_* functions are deprecated
You have error $cars[$i]] and need change $cars[$i]
$carGroups = mysql_query("INSERT INTO car_groups VALUES('$company','$cars[$i]]')");
fix php for you with good sql
$cars= $_POST['cars'];
echo $cars;
foreach($cars as $i => $cars_name){
echo $cars_name;
$carGroups = mysql_query("INSERT INTO car_groups SET `fieldcompany`='{$company}', `fieldcars`='{$cars_name}'");
}
I'm retaining values in form elements after a form submit. I've got it to work fine with a select box using the following:
<select name="BranchManager" class="formfield" id="BranchManager"onchange="document.forms[0].submit();SEinit();"><option value="">-- Select Manager --</option>
<?php
$area = $_POST['Area'];
if ($area); {
$BMquery = "SELECT DISTINCT Branch_Manager FROM Sales_Execs WHERE AREA = '$area' ".
"ORDER BY Branch_Manager";
$BMresult = mysql_query($BMquery);
while($row = mysql_fetch_array($BMresult))
{
echo "<option value=\"".$row['Branch_Manager']."\">".$row['Branch_Manager']."</option>\n ";
}
}
$branchmanager = $POST['BranchManager'];
?>
<script type="text/javascript">
document.getElementById('BranchManager').value = <?php echo json_encode(trim($_POST['BranchManager']));?>;
</script>
Which works fine (apologies if it isn't the cleanest/most efficient code, I'm doing my best!) The next field is a text field that needs to be populated based off the Branch Managers name above. So I've used :
<input name="BranchNum" type="text" class="formfield" id="BranchNum" size="3" maxlength="3" />
<?php
$bm = $_POST['BranchManager'];
if ($bm); {
$BNumquery = "SELECT DISTINCT BRANCH_NUM FROM Sales_Execs WHERE Branch_Manager = '$bm' ";
$BNumresult = mysql_query($BNumquery);
}
$branchnum = $POST['BranchNum'];
?>
<script type="text/javascript">
document.getElementById('BranchNum').value = <?php echo json_encode($BNumresult);?>;
</script>
Which isn't working... where am I going wrong here?
why are you having semicolons after if condition checks?
if ($bm);
if ($area);
This will always terminate the statement and whatever is in the curly braces will always get executed irrespective of the value in $bm or $area
You need mydql_fetch functions to retrive data from $result.
if($row = mysql_fetch_array($BNumresult))
$branchNum = $row[BRANCH_NUM];
why are you using json_encode when your input tag has size = 3?
You need to put value="<? echo $variableName; ?>" inside the input field
The reason is because a) you're not echoing, and b) you must echo in a different spot than in a select. You must echo in the value portion of the input.
<?php
$bm = mysql_real_escape_string($_POST['BranchManager']);
if ($bm) {
$BNumquery = "SELECT DISTINCT BRANCH_NUM FROM Sales_Execs WHERE Branch_Manager = '$bm' ";
$BNumresult = mysql_query($BNumquery);
}
$branchnum = $POST['BranchNum'];
?>
<input name="BranchNum"
type="text"
class="formfield"
id="BranchNum"
size="3"
maxlength="3"
value="<?php echo htmlspecialchars($branchnum); ?>" />
As per the comments, they are correct; you should not be using mysql_*. Instead, look at PDO; though this is outside the scope of your question.