Php 7 error for a simple form getting Object of class mysqli could not be converted to int - php-7

The code gets error saying Object of class mysqli could not be converted to int. When i try to load the page i get this error,i checked other questions but it was not helpful
<?php
$conn = mysqli_connect("localhost", "root", "", "coinlion");
if ($conn == 0) {
echo "could not connect";
} else {
mysqli_select_db("coinlion");
echo "connected";
}
?>
<form method="post">
<style>
#newlink {
width: 600px
}
</style>
<div id="newlink">
<div>
<table border=0>
<tr>
<td> Link URL:</td>
<td><input type="text" name="linkurl" value="" required></td>
</tr>
<tr>
<td> Link Description:</td>
<td><textarea name="linkdesc" cols="50" rows="5" required></textarea>
</td>
</tr>
</table>
</div>
</div>
<p>
<br>
<input type="submit" name="submit1">
<input type="reset" name="reset1">
</p>
php code
<?php
if (isset($_POST['submit1'])) {
$linkurl = $_POST['linkurl'];
$linkdesc = $_POST['linkdesc'];
mysqli_query($conn, "insert into
test(linkurl,linkdesc)values('$_POST[linkurl]','$_POST[linkdesc]')");
echo "data inserted";
}
?>

Well the issue was with the line if($conn==0) ,This value was taken as an integer so the error occured and an Connection variable to Mysqli_select_db was missing , after fixing it , the code ran successfully

Related

How to post table cell value in a Razor page

Looking for ideas on how to pass the 'RecordId' value for the specific row in the table, when the 'view details' button for that row is pushed. This project is .NET Core 5 Web app with Razor pages. I'm not set on the button, open to other ideas to post the value and display the details.
Here is snippet of code generating the table rows:
#foreach(var x in Model.masterrecords)
{
<tr class="border text-center">
<td>#x.RecordId</td>
<td>#x.FirstName</td>
<td>#x.LastName</td>
<td>#x.NumberOfRecords</td>
<td>
<input type="submit" asp-page-handler="ViewDetailsBtn" value="view details" class="btn btn-sm" />
</td>
</tr>
}
Try to use a form to post RecordId:
#foreach (var x in Model.masterrecords)
{
<tr class="border text-center">
<td>#x.RecordId</td>
<td>#x.FirstName</td>
<td>#x.LastName</td>
<td>#x.NumberOfRecords</td>
<td>
<form method="post" asp-page-handler="ViewDetailsBtn" asp-route-RecordId=#x.RecordId>
<input type="submit" value="view details" class="btn btn-sm" />
</form>
</td>
</tr>
}
handler(If your RecordId is string type,try to use string RecordId):
public void OnPostViewDetailsBtn(int RecordId)
{
}
result:

Posting to Servlet From Two Forms Within HTML Page

Solution: Just added null default values to the HTML parameters. Seems to have solved it!
So I have an admin page (HTML) with two forms. First form is to add to an SQL database and has 4 input boxes, second form deletes from a SQL database and has 1 input box. If I use the first form, everything is OK. But if I leave the first form empty and just put a value for the 2nd form, I get an NumberFormatException for String = "" I can only assume because the first form is empty. I'm trying to have the user either fill the first form and leave the 2nd empty, or vice versa. Any advice on how to better approach this would be great.
This is my HTML page
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Admin</title>
<link rel="stylesheet" href="myStyle.css">
</head>
<body>
<h1>Welcome to the admin page.</h1>
<h2>Please enter the new product to be added:</h2>
<form action="eShop" method="Post">
<table class="tableBox">
<tr>
<td align="left"><b>Item Code:</b></td>
<td align="left"><input type="number" name="code"></td>
</tr>
<tr>
<td align="left"><b>Item Name:</b></td>
<td align="left"><input type="text" name="name"></td>
</tr>
<tr>
<td align="left"><b>Item Price:</b></td>
<td align="left"><input type="number" name="price"></td>
</tr>
<tr>
<td align="left"><b>Is Taxable:</b></td>
<td align="left"><input type="text" name="taxable"></td>
<tr>
<td align="center" colspan="2"><input type="submit" name="action" value="Update!"></td>
</tr>
</table>
</form>
<h2>Enter the code of the product you want to delete:</h2>
<form action="eShop" method="Post">
<table class="tableBox">
<tr>
<td align="left"><b>Item Code:</b></td>
<td align="left"><input type="number" name="code"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" name="action" value="Delete!"></td>
</tr>
</table>
</form>
</body>
</html>
This is the section in my servlet that gets the parameters
else if(request.getParameter("action").equals("Delete!")) {
DataAccessImpl temp = new DataAccessImpl();
int code = Integer.parseInt(request.getParameter("code"));
temp.deleteItem(code);
}
This is the DataAccessImpl class used in the servlet, particularly the method that will delete the row.
public void deleteItem(int code) {
connect();
String deleteStm = ("DELETE FROM ProductCatalogue WHERE code = " + code);
PreparedStatement pstm = null;
System.out.println("Beginning to delete product from database...");
try {
pstm = conn.prepareStatement(deleteStm);
System.out.println("Delete complete!");
}
catch(SQLException e) {
e.printStackTrace();
}
}
in first form name your submit button like this:
<input type="submit" name="updateAction" value="Update!">
in second form name your submit button like this:
<input type="submit" name="deleteAction" value="Delete!">
Servlet Code should be like this:
else if(request.getParameter("deleteAction").equals("Delete!")) {
DataAccessImpl temp = new DataAccessImpl();
int code = Integer.parseInt(request.getParameter("code"));
temp.deleteItem(code);
}

How do I include html in C# block and C# in html block in the following View?

I have my view here and it throws errors, either there is missing {} block or an "eternal components throws and exception" when I remove those if/else block along with #: the code works just like before so how do I include html code in a C# block and C# code in html block in the following view:
#model IEnumerable<ecomm2.Models.HomeSearchResultsViewModel>
#{
if (Model.Count < 1)
{
#:<p style="color:red">Item not found</p>
}
else
{
#:<table style="background-color:#f7f7f7;width:100%; border:0px solid black;">
foreach (var item in Model) {
#:<tr style="border:1px solid #bbb9b9;">
#: <td style="width:177px;">
#: <img src="~/Content/images/meter.jpeg" alt="Alternate Text" style="height:177px;width:177px;padding:10px;"/>
#:</td>
#:<td style="width:100%;padding-left:2px;float:left;padding-top:20px;border:0px solid black;font-size:medium">
<span>
#Html.ActionLink(item.ProductLineName, "GetProductDetails", "Product", new { id = item.Id }, new { }) <br />
#: </span>
#:<span style="font-size:small">By #Html.ActionLink(item.BrandName, "GetProductByBrandName", new { id=item.BrandName})</span><br />
#:<span style="font-size:x-small">
#Html.ActionLink(item.CategoryName, "GetProductsByCategoryName", new { id=item.CategoryName}) | Stock Count: #Html.DisplayFor(modelItem => item.StockCount)
</span><br />
#using(Html.BeginForm("AddToCart", "Cart", new { id=item.Id}))
{
<fieldset>
<input type="submit" name="name" value="Add to Cart" class="btn btn-default"/>
</fieldset>
}
</td>
<td style="width:20%;color:#00b02f;font-weight:bold;padding-top:20px;float:none;padding-left:20px;">
LKR: #Html.DisplayFor(modelItem => item.ListPrice)
</td>
</tr>
}
#:</table>
}
}
Here is the original code that threw the error
External component has thrown an exception.
Here is the original code
#model IEnumerable<ecomm2.Models.HomeSearchResultsViewModel>
#if (Model.Count < 1)
{
<p style="color:red">Item not found</p>
}
else
{
<table style="background-color:#f7f7f7;width:100%; border:0px solid black;">
#foreach (var item in Model) {
<tr style="border:1px solid #bbb9b9;">
<td style="width:177px;">
<img src="~/Content/images/meter.jpeg" alt="Alternate Text" style="height:177px;width:177px;padding:10px;"/>
</td>
<td style="width:100%;padding-left:2px;float:left;padding-top:20px;border:0px solid black;font-size:medium">
<span>
#Html.ActionLink(item.ProductLineName, "GetProductDetails", "Product", new { id = item.Id }, new { }) <br />
</span>
<span style="font-size:small">By #Html.ActionLink(item.BrandName, "GetProductByBrandName", new { id=item.BrandName})</span><br />
<span style="font-size:x-small">
#Html.ActionLink(item.CategoryName, "GetProductsByCategoryName", new { id=item.CategoryName}) | Stock Count: #Html.DisplayFor(modelItem => item.StockCount)
</span><br />
#using(Html.BeginForm("AddToCart", "Cart", new { id=item.Id}))
{
<fieldset>
<input type="submit" name="name" value="Add to Cart" class="btn btn-default"/>
</fieldset>
}
</td>
<td style="width:20%;color:#00b02f;font-weight:bold;padding-top:20px;float:none;padding-left:20px;">
LKR: #Html.DisplayFor(modelItem => item.ListPrice)
</td>
</tr>
}
</table>
}
In the Index view it calls the partial page "_SearchResultList" here is the video that shows all
None of your #: code is necessary. The reason you get the exception is that IEnumerable<T> does not have a property Count so your if block throws an exception. Change you code to
if (Model.Count() < 1)
of better, use
if (Model.Any())
and swap the code in the if and else blocks

Notice: Undefined index: province in D:\wamp\www\PDO\sampleCRUD\update.php on line 13?

I have here the page for update.php my problem was I keep getting this error
Notice: Undefined index: province in D:\wamp\www\PDO\sampleCRUD\update.php on line 13
every time I select this value "---" in my dropdown list it keeps showing this error but if select the rest of the list in the dropdown there's no error I tried to set the province but I still getting this error.
here my code for update.php
<?php
include_once 'dbconfig.php';
$username = isset($_GET['username']) ? $_GET['username'] : '';
$password = isset($_GET['password']) ? $_GET['password'] : '';
$province = isset($_GET['province']) ? $_GET['province'] : '';
if(isset($_POST['btn-update']))
{
$user_id = $_GET['user_id'];
$username = $_POST['username'];
$password = $_POST['password'];
$province = $_POST['province'];
if($crud->update($user_id,$username,$password,$province))
{
echo "<script type='text/javascript'>alert('Record was updated Successfully!');</script>";
}
else
{
echo "<script type='text/javascript'>alert('Updating Failed!'); </script>";
}
}
if(isset($_GET['user_id']))
{
$user_id = $_GET['user_id'];
extract($crud->getID($user_id));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<title>Survey Update Landholdings</title>
<link rel="shortcut icon" href="image/icon.ico"/>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<div class="container">
<p><strong>Survey Update</strong></p>
<br />
<div id="Survey-Update">
<form method='post'>
<table class='table table-bordered'>
<tr>
<td>First Name</td>
<td><input type='text' name='username' class='form-control' value="<?php echo $username; ?>" required></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type='text' name='password' class='form-control' value="<?php echo $password; ?>" required></td>
</tr>
<tr>
<td>Province</td>
<?php
include_once 'dbconfig.php';
$sql = "SELECT username FROM sample";
$stmt = $DB_con->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($stmt->rowCount() > 0)
{
?>
<td><select class='form-control' name='province'>
<option selected="selected" disabled>---</option>
<?php foreach ($results as $row)
{
?>
<option value="<?php echo $row['username']; ?>"><?php echo $row['username']; ?></option>
<?php
}
?>
</select>
<?php
}
?>
</td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary" name="btn-update">
<span class="edit"></span>Update</button>
CANCEL
</td>
</tr>
</table>
</form>
</div>
the error is in this line
$province = $_POST['province'];
Change this line
<option selected="selected" disabled>---</option>
to
<option selected="selected" value="" disabled>---</option>
to send an empty value for province instead of none.
Edit: the above code does not work.
The issue here is the disabled and selected="selected". It seems, disabled options don't get submitted.
If you want to stick with disabled and selected I suggest using
$province = isset($_POST['province']) ? $_POST['province'] : '';

cant update DB with PDO/SQL it make fields blank

My problem is that when i try to update my DB table (hold) it makes all fields blank.
im using three files a config to connect.This is the config.php file.
<?php
$hostname='localhost';
$username='root';
$password='kv5772';
try {
$db = new PDO("mysql:host=$hostname;dbname=gade",$username,$password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
echo 'Connected to Database<br/>';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Then there is a print page print.php. With a delete button. It works. But the edit dont.
<?php include "config.php" ?>
<?php
$sql = "SELECT * FROM hold";?>
<table>
<tr>
<td>hold</td>
<td>leder</td>
<td>telefon</td>
<td>mail</td>
</tr>
<?php
foreach($db->query($sql) as $row){
?>
<tr>
<td><input name="hold" type="text" placeholder="<?php echo "{$row['hold']}";?>"/></td>
<td><input name="leder" type="text" placeholder="<?php echo "{$row['leder']}";?>"/></td>
<td><input name="telefon" type="text" placeholder="<?php echo "{$row['telefon']}";?>"/></td>
<td><input name="email" type="text" placeholder="<?php echo "{$row['email']}";?>"/></td>
<td>
<form action="formupdatecode.php" method="post">
<input type="hidden" name="action" value="edit" />
<input type="hidden" name="id" value="<?php echo $row['id'] ?>" />
<input type="submit" value="edit" />
</form>
</td>
<td>
<form action="slet.php" method="post">
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<?php echo $row['id'] ?>" />
<input type="submit" value="delete" />
</form>
</td>
</tr>
</table>
<?php
}
$db = null;
?>
The edit button is connected to formupdatecode.php.
<?php include "config.php" ?>
<?php
$sql = "UPDATE hold SET hold = :hold,
leder = :leder,
telefon = :telefon,
email = :email
WHERE id = :id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
$stmt->bindParam(':hold', $_POST['hold'], PDO::PARAM_STR);
$stmt->bindParam(':leder', $_POST['leder'], PDO::PARAM_STR);
$stmt->bindParam(':telefon', $_POST['telefon'], PDO::PARAM_STR);
$stmt->bindParam(':email', $_POST['email'], PDO::PARAM_STR);
if($stmt->execute()){
echo "Successfully redigeret hold";
}// End of if profile is ok
else{
print_r($stmt->errorInfo()); // if any error is there it will be posted
$msg=" Database problem, please contact site admin ";
}
?>
Your HTML code has a problem with the location of the input tags.
Your edit/input fields are located outside the form and will not be included in the POST back to the server.