{"id":5539,"date":"2022-08-29T09:26:02","date_gmt":"2022-08-29T03:56:02","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/"},"modified":"2022-08-29T09:26:02","modified_gmt":"2022-08-29T03:56:02","slug":"solved-undefined-index-inside-a-while-php","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/","title":{"rendered":"[Solved] Undefined Index inside a While PHP"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-36232518\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"36232518\" data-parentid=\"36231616\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The field &#8220;candidateid&#8221; should be integer data type, but you are enclosed this field value with <strong>&#8221;(single quotes)<\/strong> in the update query?<\/p>\n<pre><code>$sql = \"UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = '$candidateid'\";\n<\/code><\/pre>\n<p>if it is an integer datatype then you should remove the single quote<\/p>\n<pre><code>$sql = \"UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = $candidateid\";\n<\/code><\/pre>\n<p>and in MySQL every field names are case sensitive, so as you told the field names are <\/p>\n<blockquote>\n<p>candidateid, candidatename, position, numberofvotes<\/p>\n<\/blockquote>\n<p>so, you should use these names when you retrieving the values as well<\/p>\n<pre><code>    &lt;?php\n             if(isset($_POST['update'])) {\n                $dbhost=\"localhost\";\n                $dbuser=\"root\";\n                $dbpass=\"\";\n                $candidateid = $_POST['candidateid'];\n                $conn = mysql_connect($dbhost, $dbuser, $dbpass);\n\n                if(! $conn ) {\n                   die('Could not connect: ' . mysql_error());\n                }\n\n                $candidateid = $_POST['candidateid'];\n\n\n                $sql = \"UPDATE candidate_info SET numberofvotes = numberofvotes + 1 WHERE candidateid = '$candidateid'\" ;\n                mysql_select_db('election2016');\n                $retval = mysql_query( $sql, $conn );\n\n                if(! $retval ) {\n                   die('Could not update data: ' . mysql_error());\n                }\n                echo \"Updated data successfully\\n\";\n\n                mysql_close($conn);\n             }\n    ?&gt;\n&lt;html&gt;\n    &lt;center&gt;\n    &lt;font size=\"2\" face = \"century gothic\"&gt;\n    &lt;?php\n    $con=mysqli_connect(\"localhost\",\"root\",\"\",\"election2016\");\n    \/\/ Check connection\n    if (mysqli_connect_errno())\n    {\n    echo \"Failed to connect to MySQL: \" . mysqli_connect_error();\n    }\n    $result = mysqli_query($con,\"SELECT * FROM candidate_info\");\n    ?&gt;\n    &lt;form method = \"post\" action = \"&lt;?php $_PHP_SELF ?&gt;\"&gt;\n    &lt;?php\n    echo \"&lt;table border=\"1\"&gt;\n    &lt;tr&gt;\n    &lt;th&gt;Candidate Name&lt;\/th&gt;\n    &lt;th&gt;Position&lt;\/th&gt;\n    &lt;th&gt;Vote&lt;\/th&gt;\n    &lt;th&gt;Number of Votes&lt;\/th&gt;\n    &lt;\/tr&gt;\";\n    while ($row = mysqli_fetch_array($result)) {\n        echo \"&lt;tr&gt;\";\n        echo \"&lt;td&gt;\" . $row['candidatename'] . \"&lt;\/td&gt;\";\n        echo \"&lt;td&gt;\" . $row['position'] . \"&lt;\/td&gt;\";\n        echo \"&lt;td&gt;&lt;input type=\"radio\" name=\"candidateid\" value=\"\".$row[\"candidateid'].\"' &gt;\";\n        echo \"&lt;td&gt;\" . $row['numberofvotes'] . \"&lt;\/td&gt;\";\n    }\n    echo \"&lt;\/table&gt;\";\n    mysqli_close($con);\n    ?&gt;\n\n    &lt;br&gt;\n    &lt;br&gt;\n    &lt;input name = \"update\" type = \"submit\" id = \"update\" value = \"update\"&gt;\n    &lt;\/form&gt;\n    &lt;\/center&gt;\n    &lt;\/font&gt;\n    &lt;\/html&gt;\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">16<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Undefined Index inside a While PHP <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The field &#8220;candidateid&#8221; should be integer data type, but you are enclosed this field value with &#8221;(single quotes) in the update query? $sql = &#8220;UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = &#8216;$candidateid'&#8221;; if it is an integer datatype then you should remove the single quote $sql = &#8220;UPDATE candidate_info SET numberofvotes = &#8230; <a title=\"[Solved] Undefined Index inside a While PHP\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/\" aria-label=\"More on [Solved] Undefined Index inside a While PHP\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[339],"class_list":["post-5539","post","type-post","status-publish","format-standard","hentry","category-solved","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Undefined Index inside a While PHP - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Undefined Index inside a While PHP - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The field &#8220;candidateid&#8221; should be integer data type, but you are enclosed this field value with &#8221;(single quotes) in the update query? $sql = &quot;UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = &#039;$candidateid&#039;&quot;; if it is an integer datatype then you should remove the single quote $sql = &quot;UPDATE candidate_info SET numberofvotes = ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-29T03:56:02+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Undefined Index inside a While PHP\",\"datePublished\":\"2022-08-29T03:56:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/\"},\"wordCount\":85,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/\",\"name\":\"[Solved] Undefined Index inside a While PHP - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-29T03:56:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Undefined Index inside a While PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Undefined Index inside a While PHP - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Undefined Index inside a While PHP - JassWeb","og_description":"[ad_1] The field &#8220;candidateid&#8221; should be integer data type, but you are enclosed this field value with &#8221;(single quotes) in the update query? $sql = \"UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = '$candidateid'\"; if it is an integer datatype then you should remove the single quote $sql = \"UPDATE candidate_info SET numberofvotes = ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/","og_site_name":"JassWeb","article_published_time":"2022-08-29T03:56:02+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Undefined Index inside a While PHP","datePublished":"2022-08-29T03:56:02+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/"},"wordCount":85,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/","url":"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/","name":"[Solved] Undefined Index inside a While PHP - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-29T03:56:02+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-undefined-index-inside-a-while-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Undefined Index inside a While PHP"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/5539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=5539"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/5539\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=5539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=5539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=5539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}