I have a error on my php code. I will insert a new row
And i get the error: wrong parameter count in mysql_query
<?php
include('../sec/inc_mysql_connect.php');
include 'googledistance.class.php';
$sql = "SELECT VVBnummer, Adres, Postcode FROM tblscheidsrechters";// echo($sql);
$result = mysql_query($sql);
$sql_sh = "SELECT ID, SporthalAdres, Postcode FROM tblsporthal"; //echo('<br>' . $sql_sh);
$result_sh = mysql_query($sql_sh);
while($record = mysql_fetch_array($result))
{
while($record_sh = mysql_fetch_array($result_sh))
{
$fromAddress = $record['Adres'] . ',' . $record['Postcode']; //echo($fromAddress . '<br>');
$toaddress = $record_sh['SporthalAdres'] . ',' . $record_sh['Postcode']; //echo($toaddress . '<br>');
$gd = new GoogleDistance($fromAddress, $toaddress);
$vvb = $record['VVBnummer'];
$shid = $record_sh['ID'];
$afstand = $gd->getDistance();
$tijd = $gd->getDuration();
?>
<body>
<p>Scheidsrechter: <?php echo($record['VVBnummer']); ?></p>
<p>Sporthal: <?php echo($record_sh['ID']); ?></p>
<p>Afstand in km (h/t): <?php echo $gd->getDistance()/1000 ; ?></p>
<p>Tijd in minuten: <?php echo $gd->getDuration()/60; ?></p>
<p>Gevonden oorsprong: <?php echo $gd->getOrigin(); ?></p>
<p>Gevonden bestemming: <?php echo $gd->getDestination(); ?></p>
<hr />
</body>
<?php
$sql = "INSERT INTO klvv_sr_afstand_sh ( vvb_nr_sr, shid, afstand, tijd) VALUES('$vvb', '$shid', '$afstand', '$tijd')"; echo($sql);
$record = mysql_query();
}
}
?>
The error happens when i will insert the row
Thx for the help
You'd need to pass the query to the server:
$record = mysql_query($sql);
For clarification: in your code you stated $record = mysql_query();
Related
<?php
$connect = mysqli_connect($hostname, $username, $password, $database);
$query = 'select stopname from greenline';
$result = mysqli_query($connect, $query);
?>
<html>
<body>
<form action="try.php" method="get">
<?php
echo "<select name='myselect'>";
while ($row1 = mysqli_fetch_array($result)):;
echo '<option value=" ' . $row1[0] . ' " name="row" >' . $row1[0] . '</option>';
endwhile;
echo "</select>";
?>
<input type="submit" name="submit"/>
</form>
<?php
if (isset($_GET['submit']))
{
$variable = $_GET['myselect'];
$query1 = 'select placeno from greenline where stopname = " ' . $variable . '"';
$result1 = mysqli_query($connect, $query1) or die(mysql_error());
$row2 = mysqli_fetch_assoc($result1);
echo "success";
$var = $row2['placeno'];
echo " this is $var";
} ?>
</body>
</html>
I have done as above in sublime.. it is not extract the variable value in our query.could you please give a solution.. $var is not being displayed
You have ; right after while. This makes your loop a no-op, basically.
Also remove : at the same place.
You have an extra space in the query:
$query1='select placeno from greenline where stopname = " '.$variable.'"';
^
Remove that space and it will work.
But it would be better if you used a prepared statement instad of substituting variables.
$stmt = mysqli_prepare($connect, 'select placeno from greenline where stopname = ?');
mysqli_stmt_bind_param($stmt, "s", $variable);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $placeno);
mysqli_stmt_fetch($stmt);
echo "this is $placeno";
You're also adding extra spaces in the html:
echo '<option value=" ' . $row1[0] . ' " name="row" >' . $row1[0] . '</option>';
^ ^
Get rid of these spaces as well.
<?php
include 'connection.php';
if(isset($_POST['val'])){
$a = $_POST['val'];
$sql = "SELECT * FROM car_details where id= '".$a."'";
$retval = mysql_query( $sql, $con );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
$ID = $row['ID'];
$Name = $row['Car_Name'];
$Category = $row['Category'];
$Transmision = $row['Transmision'];
$Seats = $row['seats'];
$EnginePower = $row['EnginePower'];
$EngineCapacity = $row['EngineCapacity'];
$MaxSpeed = $row['MaxSpeed'];
$TankCapacity = $row['TankCapacity'];
$Car_Details = $row['Car_Details'];
$img = $row['image_path'];
}
}
?>
its php script getting data from db but it not set to html tag
<body>
<table id="car-tbl">
<div id="name"><center><h3><?php echo $Name; ?></h3></center></div><br />
<tr><td>Category</td><td> </td><td>Sport Car</td></tr>
<tr><td>Transmission</td><td> </td> <td>Automatic</td></tr>
<tr><td>Seats</td> <td> </td><td>2</td></tr>
<tr><td>Engine Power</td><td> </td> <td>420 hp</td></tr>
<tr><td>Engine Capacity</td> <td> </td><td>4200cc</td></tr>
<tr><td>Max Speed</td> <td> </td><td>301 km/h</td></tr>
<tr><td>Tank Capacity</td><td> </td><td>75 lt</td></tr>
<tr><td>GPS</td><td> </td><td>Yes</td></tr>
</table>
</body>
php script is outside the body of html and I want to add these PHP data variables in html tables as you can see I have implemented some php code into the tag but it is not working , but it generating that it is undefined variable
Help me out
please try below code
include 'connection.php';
if(isset($_POST['val'])){
$a = $_POST['val'];
$sql = "SELECT * FROM car_details where id= '".$a."'";
$retval = mysql_query( $sql, $con );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "<html><body><table id='car-tbl'><div id='name'><center><h3>";
echo $Name = $row['Car_Name'];
echo "</h3></center></div><br />";
echo "<tr><td>Category</td><td> </td>";
echo $Category = $row['Category'];
echo "</tr>";
echo "</table></body></html>";
}
}
I am working on a directory where some of the listings have a images associated with them and others do not. I am wondering how I can write a loop within a loop to get my results.
Example, User selects state they want results from, query goes to DB requesting all listings in that state.
<?php
if (isset($_POST['searchButton'])) {
$state = $_POST['state'];
$query = "SELECT * FROM directory LEFT JOIN directory_images ON directory.id = directory_images.user_id WHERE directory.state = '$state' ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) == 0) {
echo "<p>Sorry, there are no listings in '$state', check back soon!</p>\n";
}
else
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['id'];
$name = $row['name'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$has_support_pics = $row['file_name'];
?>
<h4><?php echo $name ?></h4>
<p><?php echo $address ?><br/>
<?php echo $city . ' ' . $state . ', ' . $zip; ?><br/>
</p>
<?php
// check to see if ID has extra images
if (isset($has_support_pics)) {
$query2 = "SELECT file_name FROM directory_images WHERE user_id = '$id'";
$result2 = mysql_query($query2) or die(mysql_error());
echo $query2.'<br/>';
?>
<ul class="support_images">
<?php
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
$support_image = $row['file_name'];
echo $support_image.'<br/>';
}
?>
</ul>
</div>
<br/>
</div>
<?php
}
echo "<hr/>";
}
}
?>
Do NOT run queries in loops - use a join.
Here is a tutorial: http://thewebmason.com/tutorial-parent-child-lists/
I have a multiple select list (links) which posts values to $links. I then want to run a query on table 'link' returning records that match values in $links. I am using the following code, but not getting any results:
<select name="links[]" size="9" multiple="multiple" id="links">
<?php
$query = mysql_query("SELECT * from link ORDER BY link_title ASC");
for($i=0;$i<mysql_num_rows($query);$i++) {
$row=mysql_fetch_assoc($query);
?>
<option value="<?php echo $row['link_pk']; ?>"><?php echo $row['link_title']; ?></option>
<?php
}
?>
</select>
And the submit code:
$author_pk = $_GET['author_pk'];
$title = $_POST['title'];
$topic_introduction = $_POST['topic_introduction'];
$selected_topic = $_POST['selected_topic'];
$links = $_POST['links'];
$majors = $_POST['majors'];
$majors_string = implode(",", $majors);
$sub_discipline = $_POST['sub_discipline'];
if(isset($_POST['submit'])){
$query_links = "SELECT * FROM link WHERE link_pk IN ('.implode(',',$links).')";
$result_links = mysql_query($query_links, $connection) or die(mysql_error());
while ($row_links = mysql_fetch_assoc($query_links)){
$topic_links = array();
$topic_links[$row_links['url']] = $row_links;
} if($result_links){
$topic = $topic_introduction . '<p>' . $topic_links;
$query = "INSERT INTO topic (topic_pk,title,topic,majors,sub_discipline_fk,author_fk,created)
VALUES ('','$title','$topic','$majors_string','$sub_discipline','$author_pk',NOW())";
$result = mysql_query($query, $connection) or die(mysql_error());
if($result){
$message = "- The topic '" . $title . "' has been created";
}
}
}
This line is wrong:
while ($row_links = mysql_fetch_assoc($query_links)){
... because $query_links is actually a string (your SQL query). You should use $result_links, instead.
Also, the $query_links string isn't being defined correctly. You have to use the same delimiter at the end of a string literal that you use at the beginning. Instead of:
$query_links = "SELECT * FROM link WHERE link_pk IN ('.implode(',',$links).')";
Try:
$query_links = 'SELECT * FROM link WHERE link_pk IN (' . implode(',', $links) . ')';
How can I get data from my database to show. I am not very experienced with PHP or MySQL.
I do not get an error message but no data shows so what am I doing wrong?
PHP
<?php
if(strlen(trim($_POST['search'])) > 0) {
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
mysql_connect ("cust-mysql-123-03", "", "");
mysql_select_db ("weezycouk_641290_db1");
if (!empty($_POST["search_string"]))
{
}
$query = "SELECT name,lastname,email FROM contact WHERE name LIKE '%$search%' AND
lastname LIKE '%$searchterm%'";
$result = mysql_query ($query);
echo mysql_error();
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
echo $row["name"];
echo $row["lastname"];
echo $row["email"];
} ?>
<?php echo $row["name"]; ?>
<br>
<?php echo $row["lastname"]; ?>
<br>
<?php echo $row["email"]; ?>
<?php
}
}
?>
It should be like this:
<?php
if(strlen(trim($_POST['search'])) > 0) {
mysql_connect ("cust-mysql-123-03", "", "");
mysql_select_db ("weezycouk_641290_db1");
$query = "SELECT name,lastname,email FROM contact WHERE name LIKE '%" . mysql_real_escape_string($_POST['search']) . "%' AND lastname LIKE '%" . mysql_real_escape_string($_POST['searchstring']) . "%'";
$result = mysql_query ($query);
echo mysql_error();
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
echo $row["name"];
echo $row["lastname"];
echo $row["email"];
} ?>
<?php echo $row["name"]; ?>
<br>
<?php echo $row["lastname"]; ?>
<br>
<?php echo $row["email"]; ?>
<?php
}
}
?>
The mysql_real_escape_string is to prevent mysql injection which is a serious risk.
Make sure the query you are executing returns record(s). You can check this by adding an echo statement which will print the query in your screen. Copy that and run it againist the database.You can use any mysql front end tools(php myadmin,mysqlyog to run the query. If there is any error in the query, you can see that then.
$query = "SELECT name,lastname,email FROM contact WHERE name LIKE '%$search%' AND
lastname LIKE '%$searchterm%'";
//the below line will print the query on the screen
echo $query;
$result = mysql_query ($query);